users
id
roles
id
name
user_roles
user_id
role_id
I would like to query all users with specific roles.
e.g. get all users that have the role 'admin'
So, you can use the whereHas from Eloquent
$users = User::whereHas('roles', function($query) {
$query->where('name', 'admin');
})->get();
See here for reference https://laravel.com/docs/5.5/eloquent-relationships
There is also the option to go at the relationship from the other direction:
Role::where('name', 'admin')->firstOrFail()->users;
If you know role ID, do this:
User::whereHas('roles', function ($q) use($roleId) {
$q->where('id', $roleId);
})
->get();
If you know only name or title of the role, do this:
User::whereHas('roles', function ($q) use($roleTitle) {
$q->where('title', $roleTitle);
})
->get();
Related
I have 3 table: tasks, users, group, and a pivot table group_user.
A task has user_id, group_id
A group_user pivot table has user_id, group_id
I want to query the tasks if the task belongs to a group of the user.
I don't want tasks from groups that the user doesn't belong to.
What I have so far:
public function index()
{
$userId = Auth::user()->id;
return TaskResource::collection(
Task::
latest()
->whereHas('group', function($query) use($userId) {
$query->where('group_id', '=', $userId); // = is wrong
})
->get()
);
}
This gives me empty results, I tried to think about it but my head hurts
You want to get the tasks that are related to the group (or groups) of a user.
I supposed the relation name between the user table and the group table is 'users', in the Group model.
$userId = auth()->id();
$tasks = Task::latest()
->whereHas('group', function($query) use ($userId) {
$query->whereHas('users', function($query) use ($userId) {
$query->where('user_id', $userId);
});
})
->get();
Ive not tested this but i imagine you could nest the where has and use something like this in your whereHas block:
$query->whereHas('user', function($q2) use ($userId) {
$q2->where('user_id', $userId);
});
I'm trying to figure out if there is a simple way to get all the users that have a role or another role.
On the official documentation of Spatie Laravel Permissions, I couldn't find this.
I'm trying like this but I get all the users.
User::role(['Individual', 'Organisation', 'Venue'])->get();
I'm also trying like this:
User::whereHas("roles", function($q) {
$q->orWhere(function ($query) {
$query->where("name", "Individual")
->where("name", "Venue")
->where("name", "Organisation");
});
})->get();
In the Db roles table I have:
you can try whereIn() to roles() ref link https://laravel.com/docs/8.x/queries#additional-where-clauses
User::with("roles")->whereHas("roles", function($q) {
$q->whereIn("name", ["Individual","Venue",'Organisation']);
})->get();
try id for better search as it is primary key
User::with("roles")->whereHas("roles", function($q) {
$q->whereIn("id", [6,7,8]);
})->get();
NOTE here name should match exact string of roles name which in your database
#Apokryfos meant you to change it like this
User::whereHas("roles", function($q) {
$q->where("name", "Individual")
->orWhere("name", "Venue")
->orWhere("name", "Organisation");
})->get();
if you instead want the users to have all roles and not one of the roles
User::whereHas("roles", function($q) {
$q->where("name", "Individual")
})->whereHas("roles", function($q) {
$q->where("name", "Venue")
})->whereHas("roles", function($q) {
$q->where("name", "Organisation")
})->get();
There is 3 tables in my database: Users, Biodata, Roles.
Users and Roles are in relation with Pivot table role_user.
Users have role student & employer.
I want all the users with role student also with biodata.
I tried many times but couldnot find the solution. Hope you guys help me
$users = User::with('roles')->with('biodata')->get();
try this:
$users = User::with('biodata')->whereHas('roles', function($query) {
$query->where('name', 'student');
})->get();
Use whereHas , which is used to where query in realtion.
$users = User::whereHas('roles', function ($query) {
$query->where('name', '=', 'student');
})->with('biodata')->get();
You're looking for this: $user = User::with('roles', 'biodata')->get();
I have two tables with belongsToMany relation: message_topics and users
The pivot table is message_topics_users and contains 2 columns: message_id and user_id.
In table message_topics, I have a field called sender_id
I'm trying to write the correct eloquent syntax to get all the records:
where message_topics.sender_id = $user_id
OR Message_topics_users.receiver_id = $user_id
I tried many things, like for instance:
$topics = MessageTopic::where('sender_id', $user_id)
->wherePivot('receiver_id', $user_id)->orderBy('sent_at','desc')->get();
Any idea?
You can use the whereHas method (or in this case the orWhereHas method):
$topics = MessageTopic::where('sender_id', $user_id)
->orWhereHas('users', function ($query) use ($user_id) {
$query->where('id', $user_id);
})
->orderBy('sent_at', 'desc')
->get();
I'm assuming you have two relationships from the topics? Since it's too arbitrary to use both columns and the same relationship... Like this
//On your MessageTopic model
public function sender(){
return $this->belongsToMany('App\User', 'message_topics_users', 'message_id', 'sender_id');
}
public function receiver(){
return $this->belongsToMany('App\User', 'message_topics_users', 'message_id', 'receiver_id'));
}
Then you can use whereHas and orWhereHas like this:
//Again assuming you have your User model loaded as $user
$topics = App\Topic::whereHas('sender', function($q) use($user){
$q->where('sender_id', '=', $user->id);
})
->orWhereHas('receiver', function($q) use($user){
$q->where('receiver_id', '=', $user->id
})
->orderByDesc('sent_at')
->get();
whereHas and orWhereHas both query the model (MessageTopic in this case) checking for the existence of the specified relationship (App\Topic::whereHas('sender')...). They also allow you to pass the constraint that you're looking for (function($q) use($user){ $q->... })
So it is basically saying "Give me ONLY the MessageTopics that have a Sender or Receiver with the id $user->id"
I am trying to display a list of users with the role of 'admin' or 'webmaster'. I can get it to work when specifying only one role but when I try to get both by adding an orWhere clause it returns all of the users regardless of their role.
$admins = User::whereHas('roles', function($q) {
$q->where('name', 'Admin');
})->paginate(25);
I've tried the following but it returns all of the users with every role.
$admins = User::whereHas('roles', function($q) {
$q->where('name', 'Admin')
->orWhere('name', 'Webmaster');
})->paginate(25);
Does anybody have any ideas on how to accomplish this? For reference I have a users table, a roles table, and a role_user table that is a many-to-many from roles to users.
Try this,
$admins = User::whereHas('roles', function($q) {
$q->whereIn('name', ['Admin', 'Webmaster'] );
})->paginate(25);