I have this table structure
users
- id
- roles
role
- id
- role
role_user
- id
- role_id
- user_id
And I added a function in the User
model which will check for Authenticated user's role. But I get it returns this error:
Undefined variable: role
//User.php
public function hasRole($role = null)
{
$hasRole = false;
$hasRole = !$this->roles->filter(function($item) {
return $item->role == $role;
})->isEmpty();
return $hasRole;
}
So I can use into something like this - maybe inside a filter
if(Auth::user()->hasRole('encoder')) {
//...
}