Create relationship in User model
public function roles()
{
return $this->belongsToMany(Role::class);
}
Create relationship in Role model
public function users()
{
return $this->belongsToMany(User::class);
}
Then you can attatch a role to a user like this:
Auth::user()->roles()->attach(id_of_the_role_from_roles_table);
and that will create a new entry in pivot table role_user and then you can make a function in user model
public function hasRole($role)
{
return $this->roles->contains('id', $role);
}
and you can call it where ever you want in your code. For example:
Auth::user()->hasRole(1);
That will return true or false and then you can do something with that info.