I'm trying to get all the roles permission which has a many to many relationship. I want to get all the permissions of a single role. Here I'm trying to use groupBy but it gives me error.
$search_role = DB::table('roles')
->join('roles_permissions','roles_permissions.role_id','roles.id')
->join('permissions','permissions.id','roles_permissions.permission_id')
->where('roles.name', 'like', "%$request->searcher%")
->orWhere('permissions.name', 'like', "%$request->searcher%")
->select('roles.name as role_name', 'permissions.name as permission_name','roles_permissions.*')
->groupBy('roles_permissions.role_id')
->get();
Error here:
roles_permissions.permission_id isn't in groupBy.
If I add that I get another column isn't in groupBy.
you don't have any aggregation operation in select statement. check my example
$search_role = DB::table('roles')
->join('roles_permissions','roles_permissions.role_id','roles.id')
->join('permissions','permissions.id','roles_permissions.permission_id')
->where('roles.name', 'like', "%$request->searcher%")
->orWhere('permissions.name', 'like', "%$request->searcher%")
->select('role_id', DB::raw('count(*) as total')) //example
->groupBy('roles_permissions.role_id')
->get();
what you are trying to do does not makes sense. Group by does not work in that way.
SELECT role_id, count(*)
from roles
group by role_id
is valid
SELECT roles.*, count(*)
from roles
group by role_id
is not. in the latter case, you generally need to add every column which is not aggregated to group by statement
Related
$tren = DB::table('suka')
->join('buku', 'buku.ISBN', '=', 'suka.ISBN')
->join('user', 'user.id_user', '=', 'suka.id_user')
->raw('count(*) as total')
->groupBy('suka.ISBN')
->orderBy('maximum')
->LIMIT(2)
->get();
I want to group a join table by ISBN and then find the maximum count of them and take 2 of the most.
here is the error message
Call to undefined method Illuminate\Database\Query\Expression::groupBy()
There is more than one ISBN column in the result set (because it exists in multiple tables). You should specify which table's ISBN column you want to group by in your group by call.
Also, try calling selectRaw instead of raw:
->selectRaw('count(*) as total')
You can read more about raw queries in this documentation.
I have 2 tables, one with different users, and the second table is an invoice table called "factures" and has a foreign key of userid, I called it client_id, which I am trying to get is the number of clients created_by a certain administrator and who have no invoices yet, here is what I tried:
$clients = User::select('id')
->where([['created_by',$membre_id],['role','Client']])
->orWhere([['updated_by',$membre_id],['role','Client']])
->whereNotExists(function($query)
{
$query->select(DB::raw('client_id'))
->from('factures')
->where('created_by',$member_id);
})->get();
but this query gives me all clients created_by $member_id without exception.
What is wrong with my query?
Did you try the following:
$clients = User::select('id')
->where(function($query) use($member_id){
$query->where([['created_by',$membre_id],['role','Client']])
->orWhere([['updated_by',$membre_id],['role','Client']])
})
->whereNotExists(function($query) use($member_id){
$query->select(DB::raw('client_id'))
->from('factures')
->where('created_by',$member_id);
})
->get();
}
This answer applied the OR condition only between the first two conditions (created_by and updated_by) and its result is AND with the third condition.
I build a blog. On my blog, I would like to get my tags but I would like to sort them by more used. I have a pivot table and I don't know how to deal with it at all.
I have a table 'tags', a table 'articles' and a table 'article_tag'.
Can you show me how it is possible to carry out such an operation with the QueryBuilder?
Thank you
You can use JOIN, GROUP BY and COUNT:
$tags = Tags::join('article_tags as at', 'at.tag_id', '=', 'tags.id')
->groupBy('tags.id')
->selectRaw('tags.*, count(*) as article_count')
->orderBy('article_count', 'desc')
->get();
Basically I want to build tihs query in Laravel, but it does not work.
SELECT films.id, films.name AS film
FROM films
WHERE films.id NOT IN
(
SELECT films.id
FROM actors, actor_film, films
WHERE actors.id = actor_film.actor_id
AND actor_film.film_id = films.id
GROUP BY films.id
)
ORDER BY films.id DESC
LIMIT 600
;
Using a "whereNotIn" I have written these two queries:
The first one get all films in the Data Base that has at least an actor associated like this:
$films_with_actors = DB::table('films')
->join('actor_film', 'actor_film.film_id', '=', 'films.id')
->join('actors', 'actors.id', '=', 'actor_film.actor_id')
->select( 'films.id')
->groupBy('films.id')
->get();
Now I want to get the films that do not have associated an actor. For that I am trying to get the ID that are not included in the previous method, like this:
$films_with_no_actors = DB::table('films')
->whereNotIn('films.id', $films_with_actors)
->orderBy('films.id', 'desc')
->take(500)
->get();
-
Any help?
I am giving you a basic solution based on the code you shared.
In laravel you have a method called pluck for retrieving an array with all the values for a given key.
Therefore, you can get only the ids for the $films_with_actors. Something like (based on your first query):
$films_with_actors = DB::table('films')
->join('actor_film', 'actor_film.film_id', '=', 'films.id')
->join('actors', 'actors.id', '=', 'actor_film.actor_id')
->select( 'films.id')
->groupBy('films.id')
->pluck('id')->toArray();
Now you have an array with the ids and you can include that array in the whereNotIn clause of your second query:
$films_with_no_actors = DB::table('films')
->whereNotIn('films.id', $films_with_actors)
->orderBy('films.id', 'desc')
->take(500)
->get();
Here is my Database structure:
Also - there is a table users and reciever_id references that table id.
I use that query to get count of each type of notifications as well as data for that type of notifications from notification_types table.
Notification::
select('notification_types.*', DB::raw('count(*) as total'))
->join('notification_types', 'notifications.type_id', '=', 'notification_types.id')
->groupBy('notifications.type_id')
->get()
What I need - is to set constraint on reciever_id, I just don't get - where should I put the where clause?
Just chain the where method anywhere before get, since your condition will be applied on the notifications table:
Notification::select('notification_types.*', DB::raw('count(*) as total'))
->join('notification_types', 'notifications.type_id', '=', 'notification_types.id')
->where('reciever_id', $receiverId)
->groupBy('type_id')
->get();
Also, there's no need with this query to group by notifications.type_id, type_id will do, because there are no ambiguities created here because there are no other columns named type_id.