I make a function
user.php
public function roles(){
return $this->belongsTo(Role::class);
}
public function hasRole($roleName){
return $this->roles()->where('name', $roleName)->exists();
}
this is the role model
class Role extends Model
{
public function users() {
return $this->belongsToMany(User::class);
}
and this one is my view with auth()->check()
authstatus.blade
@if (auth()->check() && auth()->user()->hasRole('Member'))
<div class="panel-heading">
Member
</div>
<div class="panel-body">
<p>login as Member</p>
</div>
@elseif (auth()->check() && auth()->user()->hasRole('Admin'))
<div class="panel-heading">
Admin
</div>
<div class="panel-body">
<p>login as Admin</p>
</div>
@else
<div class="panel-heading">
Guest
</div>
<div class="panel-body">
<p>login as Guest</p>
</div>
@endif
Then i get
Login as Guest
All my auth or my user always comes out as Guest even thought i LOGIN as MEMBER
My header.blade consist
<meta name="csrf-token" content="">
So i run lil test, i put only
@if (auth()->check())
<div class="panel-heading">
Member
</div>
<div class="panel-body">
<p>login as Member</p>
</div>
@else
<div class="panel-heading">
Guest
</div>
<div class="panel-body">
<p>login as Guest</p>
</div>
@endif
then i get
Login as Member
so i think the missing value is 'auth()->user()->hasRole('Member')' it seems the when i login as member, the program doesnt know what is member so when executed, but it knows Auth.
so
I run another test and aplly auth()->user()->hasRole('Member') in the blade.
i change Function hasRole from exists() to get().
public function hasRole($roleName){
return $this->roles()->where('name', $roleName)->get();
}
Now all my login user comes out as member even thought i login as a guest. Help!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire