mardi 4 juillet 2017

Laravel 5 - Use 'isAdmin' role check method when querying a model

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)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire