Order by and Group by with Laravel - php

In my current code I'm using groupBy for a specific column and also I want to order each array by order_no. With my current code I get:
"Method Illuminate\Database\Eloquent\Collection::orderBy does not
exist."
$courses = CourseTopic::select([
'course_topics.id',
'course_topics.course_id',
'course_topics.description',
'course_topics.visible',
'course_topics.name',
'course_activities.order_no',
'course_activities.activity_id',
'activity_types.table_name'
])
->where('course_id', $course_id)
->leftJoin('course_activities', 'course_activities.course_topic_id', 'course_topics.id')
->leftJoin('activity_types', 'activity_types.id', 'course_activities.activity_type_id')
->get()
->groupBy('id')
->orderBy('course_activities.order_no');

You have them after get(), which means it is now a Collection query and no longer a QueryBuilder object. There are two options:
Move it before get(), so that MySQL does the ordering and grouping:
$courses = CourseTopic::select([
'course_topics.id',
'course_topics.course_id',
'course_topics.description',
'course_topics.visible',
'course_topics.name',
'course_activities.order_no',
'course_activities.activity_id',
'activity_types.table_name'
])
->where('course_id', $course_id)
->leftJoin('course_activities', 'course_activities.course_topic_id', 'course_topics.id')
->leftJoin('activity_types', 'activity_types.id', 'course_activities.activity_type_id')
->groupBy('course_topics.id')
->orderBy('course_activities.order_no')
->get();
Or use sortBy, which is the Collection method. You'll need to use the returned column name instead of the MySQL relation
$courses = CourseTopic::select([
'course_topics.id',
'course_topics.course_id',
'course_topics.description',
'course_topics.visible',
'course_topics.name',
'course_activities.order_no',
'course_activities.activity_id',
'activity_types.table_name'
])
->where('course_id', $course_id)
->leftJoin('course_activities', 'course_activities.course_topic_id', 'course_topics.id')
->leftJoin('activity_types', 'activity_types.id', 'course_activities.activity_type_id')
->get()
->groupBy('id')
->sortBy('order_no');

Related

Laravel how to get only one record per user

In my laravel-application, I want to display all users/candidates, which have taken an education. Now it might happen, that a user/candidate has taken more than one education, so in this case, just the latest education should be displayed.
I've come to this:
$users = User::whereHas('roles', function ($q) {
$q->where('slug', 'candidate');
}
)->whereHas('educations')
->join('user_educations', 'user_educations.user_id', '=', 'users.id')
->join('educations', 'user_educations.education_id', '=', 'educations.id')
->join('education_levels', 'user_educations.education_level_id', '=', 'education_levels.id')
->select('users.id', 'users.name', 'users.surname', 'users.status', 'users.city', 'users.zipcode', 'users.birthday', 'users.avatar', 'educations.title as education', 'education_levels.title as education_level')
->where('user_educations.user_id', '=', 'users.id') // HERE IT FAILS - returns null
->first();
return response(['success' => true, "users" => $users], 200);
when I leave out the where('user_educations.user_id', '=', 'users.id')-clause, and do get() instead of first(), I get all users/candidates with educations, and also sometimes the same user multiple times, depending on how many educations he has taken.
how can I fix this?
The distinct method allows you to force the query to return distinct results, you can use it like this;
$users = DB::table('users')->groupBy('user_id')->distinct()->get();
If you want to take just the latest record, you should use "orderBy" to order your records, than pick a record with something like this :
$users = DB::table('users')->groupBy('user_id')->orderBy('created_at','desc')->distinct()->get();
You can look here for more detailed information about query builders in Laravel

How to add a where clause when using "with" on an Eloquent query in Laravel

I have a query built where I'm using "with" to include related models. However, I'm not sure how to filter those related models in a where clause.
return \App\Project::with("projectLeaders")->join('companies', 'company_id', '=', 'companies.id')
->join('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*');
Please note the with("projectLeaders") in the query. So, ProjectLeaders is a relation that brings objects of kind Employee, how can I filter in that query those "Employees" whose attribute "Lastname" is like "Smith" ?
You can implement where class both tables. Please check following code and comments.
return \App\Proyecto::with(["projectLeaders" => function($query){
$query->where() //if condition with inner table.
}])->join('empresas', 'id_empresa', '=', 'empresas.id')
->join('tipo_estado_proyecto', 'tipo_estado_proyecto.id', '=', 'proyectos.id_tipo_estado_proyecto')
->where() //if condition with main table column.
->select('empresas.*', 'tipo_estado_proyecto.nombre AS nombreEstadoProyecto', 'proyectos.*');
You can use Closure when accessing relation using with. Check below code for more details:
return \App\Project::with(["projectLeaders" => function($query){
$query->where('Lastname', 'Smith') //check lastname
}])->join('companies', 'company_id', '=', 'companies.id')
->join('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*');
You may use the where method on a query builder instance to add where clauses to the query. The most basic call to where requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. Finally, the third argument is the value to evaluate against the column.
return \App\Project::with("projectLeaders")->join('companies', 'company_id', '=', 'companies.id')
->join('project_status', 'project_status.id', '=', 'projects.status_id')
->where('lastname','=','Smith')
->select('companies.*', 'project_status.name AS statusName', 'projects.*');
Don't forget to return results with a get();
The query you have written is correct. But after building the query you need to fetch the data from database.
METHOD ONE
So adding get() method to your query:
return App\Project::with('projectLeaders')
->leftJoin('companies', 'company_id', '=', 'companies.id')
->leftJoin('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*')
->get();
METHOD TWO (with pagination)
return App\Project::with('projectLeaders')
->leftJoin('companies', 'company_id', '=', 'companies.id')
->leftJoin('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*')
->paginate(3);

Laravel 5.4 Eloquent relationship query return null using ->first()

I hope anybody can help me with this.
I have the next eloquent query:
$chats = Solicitud::with(['reservation', 'detail.subDetails', 'conversation'=>function($q){
$q->orderBy('created_at', 'DESC')->groupBy('solicitud')->first();
}])
->whereIn('idSolicitud', $r)
->skip($inicio)
->take($elementos)
->orderByRaw("field(idSolicitud, ".implode(',' , $r->toArray()).")")
->get();
What I want is get the last conversation so I use ->first() to get the last. But this return conversation:[] without data, but when I change ->first() to ->get() return all the conversation I don't know what is wrong.

Laravel Model Using Or in where Condition?

I want to get the template from user_webhook table in my database.In WHERE condition i am checking user_id,app_id and if either notify_admin or notify_customer value is 1 in user_webhook table.I am using query..
$templates= $this->where('notify_admin',1)
->orwhere('notify_customer',1)
->where('user_webhooks.user_id',$user_id)
->where('user_webhooks.app_id',$app_id)
->select( 'webhooks.id as webhook_id','webhooks.app_id','webhooks.topic','webhooks.type','webhooks.action',
'webhooks.sms_template','user_webhooks.id','user_webhooks.notify_admin',
'user_webhooks.notify_customer','user_webhooks.user_id','user_webhooks.sms_template_status',
'user_webhooks.sms_template as sms'
)
->join ('webhooks',function($join){
$join>on('webhooks.id','=','user_webhooks.webhook_id');
})
->get()
->toArray();
when i get query using DB::getQueryLog(), I found the query seems Like
select `telhok_webhooks`.`id` as `webhook_id`, `telhok_webhooks`.`app_id`,
`telhok_webhooks`.`topic`, `telhok_webhooks`.`type`, `telhok_webhooks`.`action`,
`telhok_webhooks`.`sms_template`, `telhok_user_webhooks`.`id`,
`telhok_user_webhooks`.`notify_admin`, `telhok_user_webhooks`.`notify_customer`,
`telhok_user_webhooks`.`user_id`, `telhok_user_webhooks`.`sms_template_status`,
`telhok_user_webhooks`.`sms_template` as `sms` from `telhok_user_webhooks`
inner join
`telhok_webhooks` on `telhok_webhooks`.`id` = `telhok_user_webhooks`.`webhook_id`
where `notify_admin` = ? or `notify_customer` = ? and `telhok_user_webhooks`.`user_id`
= ? and `telhok_user_webhooks`.`app_id` = ?
The result of query giving result of all app_id and user_id.
So Please tell me use of OR in where condition.
Thanks in advance.
You may chain where constraints together as well as add or clauses to the query. The orWhere method accepts the same arguments as the where method:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
Advanced usage:
Usere::where('id', 46)
->where('id', 2)
->where(function($q) {
$q->where('Cab', 2)
->orWhere('Cab', 4);
})
->get();
The whereIn method verifies that a given column's value is contained within the given array:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
More: https://laravel.com/docs/5.5/queries
Change
->where('notify_admin',1)
->orwhere('notify_customer',1)
to
->where(function($q){
$q->where('notify_admin',1)
->orWhere('notify_customer',1);
})
Without this, the orWhere will compare to all other wheres in your query instead of just comparing those two columns

Laravel OrderBy Nested Collection

I'm using a Roles package (similar to entrust). I'm trying to sort my User::all() query on roles.id or roles.name
The following is all working
User::with('roles');
This returns a Collection, with a Roles relation that also is a collection.. Like this:
I'm trying to get all users, but ordered by their role ID.
I tried the following without success
maybe because 'roles' returns a collection? And not the first role?
return App\User::with(['roles' => function($query) {
$query->orderBy('roles.id', 'asc');
}])->get();
And this
return App\User::with('roles')->orderBy('roles.id','DESC')->get();
None of them are working. I'm stuck! Can someone point me in the right direction please?
You can take the help of joins like this:
App\User::join('roles', 'users.role_id', '=', 'roles.id')
->orderBy('roles.id', 'desc')
->get();
Hope this helps!
You can make accessor which contains role id or name that you want to sort by.
Assume that the accessor name is roleCode. Then App\User::all()->sortBy('roleCode') will work.
Here's the dirty trick using collections. There might be a better way to achieve this(using Paginator class, I guess). This solution is definitely a disaster for huge tables.
$roles = Role::with('users')->orderBy('id', 'DESC')->get();
$sortedByRoleId = collect();
$roles->each(function ($role) use($sorted) {
$sortedByRoleId->push($role->users);
});
$sortedByRoleId = $sortedByRoleId->flatten()->keyBy('id');
You can sort your relations by using the query builder:
notice the difference with your own example: I don't set roles.id but just id
$users = App\User::with(['roles' => function ($query) {
$query->orderBy('id', 'desc');
}])->get();
See the Official Laravel Docs on Constraining Eager Loading
f you want to order the result based on nested relation column, you must use a chain of joins:
$values = User::query()->leftJoin('model_has_roles', function ($join)
{
$join>on('model_has_roles.model_id', '=', 'users.id')
->where('model_has_roles.model_type', '=', 'app\Models\User');})
->leftJoin('roles', 'roles.id', '=', 'model_has_roles.role_id')
->orderBy('roles.id')->get();
please note that if you want to order by multiple columns you could add 'orderBy' clause as much as you want:
->orderBy('roles.name', 'DESC')->orderby('teams.roles', 'ASC') //... ext
check my answer here:
https://stackoverflow.com/a/61194625/10573560

Categories