1

I have implemented a basic role system that uses a table 'role_user'.

On my user model I have a few methods that check the roles, one of them is:

public function isStaff()
{
    foreach ($this->roles()->get() as $role)
    {
        if ($role->id == 3)
        {
            return true;
        }
    }

    return false;
}

How can I use this method when I am querying users?

This query here:

return User::where('name', 'like', "%".$request->name."%")
    ->orWhere('email', 'like', "%".$request->name."%")
    ->whereDoesntHave('Teams', function ($query) use($teamId) {
        $query->whereId($teamId);
    })
    ->with('teams')
    ->get();

Currently returns all users, but I only wish to return users that have a role of 3 (isStaff)

Lovelock
  • 7,689
  • 19
  • 86
  • 186
  • You can use `with('isStaff')` before `User::where` Like: `User::with('isStaff)->where()` Let me know if this works! – Hiren Gohel Jul 04 '17 at 11:07

3 Answers3

1

You can using Scopes With Laravel instead of multiple methods to check for different methods.

public function scopeRole($query, $flag)
{
        return $query->where('role', $flag);
}

and then

$users= User::role(3)->get();

check the reference tutorial for Creating Dynamic Scopes

Ruchita Sheth
  • 840
  • 9
  • 27
0

it's better to do condition

return User::where('name', 'like', "%".$request->name."%")
->orWhere('email', 'like', "%".$request->name."%")
->whereDoesntHave('Teams', function ($query) use($teamId) {
    $query->whereId($teamId);
})
->whereHas('roles', function($q) use ($role_id){
    $q->where('id',$role_id);
})
->with('teams')
->get();

or also you can create a method for above query and based on param reurn result

Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
0

You can have a scope called staff in your User model, then use that to narrow down your result:

public function scopeStaff($query, $roll_id = 3) 
{
    return $query->where('role_id', '=', $roll_id)
}

So when checking (with the model) for staff roles, you can improve the function that does that:

public function isStaff($role_id = 3)
{
    return $this->role_id = $role_id ? $this : false;
}

Therefore, when using the query builder you can use the first method to narrow the result to those with the specified id, as you can see the default is 3 but will change to any value you give:

$staff_users = User::staff()->get();

Then the other one for verifying if a matched user model is a staff:

$user = User::find(1);
$is_staff = $user->isStaff(); //false or returns the same model

Hope this helps

  • This would probably work if I was storing the role id on the users table, but sadly its a pivot table. – Lovelock Jul 04 '17 at 13:15
  • @Lovelock You may try then, for your pivot if on belongs to many, you can have another method for example, `staff` then use: `$this->belongsToMany('App\User')->wherePivot('role', 3);` This is just a suggestion, you can find more inspiration from: https://laravel.com/docs/5.2/eloquent-relationships#many-to-many scroll down to **Retrieving Intermediate Table Columns** – Oluwatobi Samuel Omisakin Jul 04 '17 at 13:52