Get all users with one role or another using Eloquent - laravel-5

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);

Related

Use whereHas and query pivot table

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);
});

Spatie Laravel Permissions - How To Get Users that have one Role or another Role

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();

eloquent where id in from many to many

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();

Eloquent User Where Clause with Entrust Library

I'm trying to select all users for a company. But only users who has "admin" role status (Entrust, etc.).
User::where('company_id', Auth::user()->company_id)->hasRole('admin')->get();
The above is throwing an error. Left a bit lost on how to run such a query. Where am I going wrong with this? Very little documentation on Entrust.
You can use plain Eloquent for this. The whereHas method would generate one query:
$users = User::where('company_id', Auth::user()->company_id)
->whereHas('roles', function($query) {
$query->where('name', 'admin');
})->get();
Or, you can just get the roles and then get all of that role's users. This would generate two queries, but ultimately achieve the same thing.
$users = Role::where('name', 'admin')
->first()
->users()
->where('company_id', Auth::user()->company_id)
->get();
Think you need to get all the users having the company_id first
$users = User::where('company_id', Auth::user()->company_id)->get();
Then loop through $users and check hasRole()
foreach ($users as $user) {
if($user->hasRole('admin')){
//user is admin
}
}
UPDATE
This might be a dirty solution but you can try to do a manual query
$admin = DB::table('role_user')
->join('users', 'users.id', '=', 'role_user.user_id')
->join('roles', 'roles.id', '=', 'role_user.role_id')
->where('roles.name', 'admin')->get();

Laravel: how to query to get all records except where relation has name column = admin

I have this query
$users = User::whereHas('roles', function($q){
$q->where('name', '!=', 'admin');
})->get();
but I want to get all users including the ones that have no roles associated. Is there a way to query this?
You want to get the users where the number of roles matching 'admin' is less than one:
$users = User::whereHas('roles', function($q){
$q->where('name', 'admin');
}, '<', 1)->get();
whereHasNot is coming, but not in a release yet.

Resources