I am at the admin user, which is holding the administrator role, but when I add a condition to check if the user is admin or not it will always return false. I can’t find the problem.
This is my code:
I set up the relation in my user model, and a condition in the END OF THE CODE which will check if the user is admin or not:
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role() {
return $this->belongsTo('AppModelsRole');
}
public function isAdmin() {
if($this->role->name == 'administrator')
return true;
else
return false;
}
}
Than I created a middleware which will allow me to go in to the admin page if the user is admin else it will redirect me to the root:
2
Answers
I found my mistake, so I runed:
And It rendered this in the page:
I had them:
but this means:
The problem was that I putted an extra space when I added the role name into my database, be aware of that because its a hard mistake to see.
BE AWARE OF THE SPACING!
Now it works!!
first of all the route action should be:
than to check weather user is admin or not you can do it in middleware like this: