Jeú Casulo Jeú Casulo - 3 months ago 13
PHP Question

Call roles function from model in html view or controller

I already have a Custom Auth that works fine. But I can only use it in my routes blocking or releasing the access. But now I would like to hide or show buttons and links for some Roles and Users, something like:

@if(user==admin) // this line (Auth::user->roles()) ??
<a href="admin">Configuration</a
@endif


But sadly I cant do that no matter how I try it. Like I said I already have this User Model

public function roles()
{
return $this->belongsToMany('App\Role', 'user_role', 'user_id', 'role_id');
}

public function hasAnyRole($roles)
{
if (is_array($roles)) {
foreach ($roles as $role) {
if ($this->hasRole($role)) {
return true;
}
}
} else {
if ($this->hasRole($roles)) {
return true;
}
}
return false;
}

public function hasRole($role)
{
if ($this->roles()->where('name', $role)->first()) {
return true;
}
return false;
}


But I do not know how to call this functions to my blade view since Im a starter in laravel. So any help would be great, thank you. Bye

Answer

try this

@if(\Auth::user()->hasRole('admin')) 
<a href="admin">Configuration</a
@endif