Get the last entry in laravel - laravel

How can I display the last entry in my blade.
I have this request:
#foreach(($listContrat = \App\Stagiaire::join('carrieres', 'stagiaires.id', '=', 'carrieres.stagiaire_id')
->where('stagiaires.id', $id)
->OrderBy('id','desc')
->take(1)
->get())
as $key => $car)
I have this error message with this query
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous (SQL: select * from stagiaires

Thanks freedomn-m
The solution is:
#foreach(($listContrat = \App\Stagiaire::join('carrieres', 'stagiaires.id',
'=', 'carrieres.stagiaire_id')
->where('stagiaires.id', $id)-
>OrderBy('carrieres.id','Desc')->take(1)->get()) as $key => $car)

Related

Laravel Join with where not in gives "Column xxxxx in IN/ALL/ANY subquery is ambiguous"

I have two tables: leads and hts_patients. Both table has phone column and I want to list all leads that has no record on hts_patients table(foreign key is phone).
So far, I have tried this and it works but taking so much long time.
$countries = ['DE','TR'];
$dateS = Carbon::now()->startOfMonth()->subMonth(4);
$dateE = Carbon::now()->startOfMonth();
$leads = Lead::whereBetween('created_at',[$dateS,$dateE])->whereIn('visitor_country',$countries)
->select('id','name','phone','visitor_country','source','description','contact','created_at')->get();
$patient = HtsPatient::pluck('phone');
foreach ($leads as $key=>$item){
if (isset($item->phone)){
$data =$patient->where('phone',$item->phone)->first();
if (!empty($data)){
$leads->forget($key);
}
}
}
return view('poll.leadSecond',['leads' => $leads]);
Thing, that I want is probably something like this;
$countries = ['DE','TR'];
$dateS = Carbon::now()->startOfMonth()->subMonth(4);
$dateE = Carbon::now()->startOfMonth();
$leads = DB::table('leads')->join('hts_patients','hts_patients.phone', '=', 'leads.phone')
->whereBetween('leads.created_at',[$dateS,$dateE])->whereIn('leads.visitor_country',$countries)
->whereNotIn('phone', function($query) {
$query->select('phone')
->from('hts_patients');
})
->select('leads.id','leads.name','leads.phone','leads.visitor_country','leads.source','leads.description','leads.contact','leads.created_at')->get();
But it gives an error says
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'phone'
in IN/ALL/ANY subquery is ambiguous
Any help?
you have tow tables with column called 'phone', in your whereNotIn clause you should specify witch on you wants.
I think the result should be like:
->whereNotIn('leads.phone', function($query) {
$query->select('hts_patients.phone');
// ->from('hts_patients'); you do not need this 'from'
})

Laravel eager loading specific columns error

I have custom code where I must get specific columns from relation data:
$jobs = Job::with('user:id, name')
->where('type', 0)
->where('status', 1)
->orderBy('updated_at', 'DESC')
->get();
When I run this code Laravel return me error message:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' name' in
'field list'
How can I solve this error?
It can be done one by passing a closure function in with() as second index of array like
$jobs = Job::with(['user' => function($q){
$q->select('id','name');
}])->where('type', 0)
->where('status', 1)
->orderBy('updated_at', 'DESC')
->get();
In this code has space with('user:id, name'). Problem is space. Try this one
$jobs = Job::with('user:id,name')
->where('type', 0)
->where('status', 1)
->orderBy('updated_at', 'DESC')
->get();

Convert a many to many raw sql statement to eloquent query builder

I need to translate this working sql statement:
select model_names.name
FROM blog_posts
INNER JOIN model_names_relations
INNER JOIN model_names
ON blog_posts.id = model_names_relations.blog_post_id and model_names.id = model_names_relations.model_name_id
WHERE blog_posts.id = '12'
to laravel query builder. I'm NOT using the full orm, so I can't use the belongstomany feature. I'm restricted to the query builder.
I tried this:
$query = ( new DbSql )->db()->table( 'blog_posts' )
->join( 'model_names_relations', 'blog_post_id.id', '=', 'model_names_relations.blog_post_id' )
->join( 'model_names', 'model_names.id', '=', 'model_names_relations.model_name_id' )
->where( 'blog_posts.id', '12')
->select( 'model_names.name' )
->get();
var_dump( $query );
exit;
But it won't work I get:
protected 'message' => string 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blog_post_id.id' in 'on clause' (SQL: select model_names.name from blog_posts inner join model_names_relations on blog_post_id.id = model_names_relations.blog_post_id inner join model_names on model_names.id = model_names_relations.model_name_id where blog_posts.id = 12)' (length=357)
private 'string' (Exception) => string '' (length=0)
What would be the correct conversion syntax ?
Here is Laravel query builder
$query =DB::table('blog_posts')
->join('model_names_relations', 'blog_posts.id', '=', 'model_names_relations.blog_post_id')
->join('model_names', 'model_names.id', '=', 'model_names_relations.model_name_id')
->where('blog_posts.id', '12')
->get();
However your error means there is no 'id' in blog_post_id table.

Update on select count using eloquent

I have an eloquent query which returns the 10 most frequently found names in a table.
$topPeople = $model
->select(['name', DB::raw('COUNT(*) as count')])
->groupBy('name')
->orderBy('count', 'desc')
->take(10)
->get();
This works fine for returning results but I want to update these records rather than return the data. When I use update instead of get;
$model
->select(['name', DB::raw('COUNT(*) as count')])
->groupBy('name')
->orderBy('count', 'desc')
->take(10)
->update(['popular' => 1]);
I get the error;
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'order clause'
What is an efficient way of updating the popular field in these records?
Select the ids, then use those to perform the update. Here's an example using your code, slightly changed:
$topPeople = SomeModel
->select(['id', DB::raw('COUNT(*) as count')])
->groupBy('id')
->orderBy('count', 'desc')
->take(10)
->pluck('id');
SomeModel::whereIn('id', $topPeople)->update(['popular' => 1]);

Laravel left join showing error on 'ON' multiple condition

When i tried using left join in laravel I am getting below error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '$dat' in 'on clause' (SQL: select students.id, students.name, attendance_student.date from students left join attendance_student on students.id = attendance_student.student_id and attendance_student.date = $dat)
Basically I am trying to get attendance of all students on a particular date
My Code:
$dat = 2015-10-15;
$student = DB::table('students')
->leftJoin('attendance_student', function($join)
{
$join->on('students.id', '=', 'attendance_student.student_id');
$join->on('attendance_student.date', '=', $dat);
})
->select('students.id', 'students.name', 'attendance_student.date'
)
->get();
PLease help
Your issue: Variable scopes
Solution:
$dat = 2015-10-15;
$student = DB::table('students')
->leftJoin('attendance_student', function($join) use ($dat)
{
$join->on('students.id', '=', 'attendance_student.student_id');
$join->on('attendance_student.date', '=', $dat);
})
->select('students.id', 'students.name', 'attendance_student.date'
)
->get();
Explanation:
When accessing variables that were declared outside the scope of the lambda function (the function($join) one), make use of use statement to allow access to those variables inside the function.
Source: http://php.net/manual/en/functions.anonymous.php

Resources