Laravel 中的角色未按预期工作
Roles in Laravel aren't working as intended
我已经按照书上的规定为我的 laravel 程序设定了角色。问题出现在一个特定的实例中,用户模型中的一种方法总是 "pop-up" ... 无论用户对 role_id 有什么,方法总是显示特定角色的名称,在本例中为 "moderator"。不知道它有什么问题,或者至少我看不到它...
这是用户模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',"avatar"
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function rola(){
return $this->belongsTo("App\Roles", "id");
}
public function posts(){
return $this->hasMany("App\Post");
}
public function comments(){
return $this->hasMany("App\Comment");
}
public function isAdmin()
{//dd($user);
return $this->rola()->where('role', 'administrator');
}
public function isModerator()
{//dd($user);
return $this->rola()->where('role', 'moderator');
}
public function isUser()
{//dd($user);
return $this->rola()->where('role', 'user');
}
public function isPeon()
{//dd($user);
return $this->rola()->where('role', 'peon');
}
}
这是角色模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Roles extends Model
{
public function rola(){
return $this->hasMany("App\User", "role_id");
}
}
例如,如果只在任何视图中显示:
{{auth()->user()->rola->id}} <br> // 2 <- correct
{{auth()->user()->role_id}} // 3 <- this should be 2 also.
这是我的数据库模式的图像:
以及角色形象table:
默认情况下,每当新用户注册时,它都会自动接收 ID 为 4 的 "peon" 角色。
不确定我的错误在哪里,也许我没有看到什么......
编辑1:
这里有 link 到 github repo 供想看一看的人参考。
嗯,我认为问题出在 User.php 文件中的 rola()
函数:您正在为 belongsTo
这里的关系是 id
.
它应该是 role_id
因为这是键 Eloquent 会尝试映射到角色模型的 id
。 See the documentation 获取更多信息
问题出在您的 rola()
函数中。
您在 belongsTo
中使用了错误的外键。它应该是 role_id
,而不是 id
(目前是)。
这是一个经过一些改进的代码片段:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',"avatar"
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function rola(){
return $this->belongsTo("App\Roles", "role_id");
//Or just return $this->belongsTo(Roles::class);
}
public function posts(){
return $this->hasMany("App\Post");
}
public function comments(){
return $this->hasMany("App\Comment");
}
public function isAdmin()
{//dd($user);
return $this->hasRole('admin');
}
public function isModerator()
{//dd($user);
return $this->hasRole('moderator');
}
public function isUser()
{//dd($user);
return
return $this->hasRole('user');
}
public function isPeon()
{//dd($user);
return $this->hasRole('peon');
}
public function hasRole($role)
{
return $this->rola->role === $role;
}
}
我建议您将角色模型中的 rola
函数重命名为 users
,因为它 return 所有具有某种角色的用户。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Roles extends Model
{
public function users(){
return $this->hasMany(User::class, 'role_id');
}
}
还有一件事:看看this package。它确实可以帮助您处理用户、权限和权限组。
希望对您有所帮助。
要改进您的应用程序,请不要为您拥有的每个角色使用一个函数。
假设您在角色 table 上有 100 个角色,使用当前代码,您还需要 100 个函数,例如 isAdmin
或 isSomething
.
添加如下内容:
public function hasRole($roleName)
{
return $this->rola()->where('role', $roleName);
}
/**
* Check if the user has any of the given roles.
*/
public function hasAnyRole(array $roleNames)
{
$userRole = $this->rola;
return in_array($userRole->name, $roleNames);
}
现在您可以检查角色:
$user->hasRole('admin'); //If the user has the admin role, returns true. Otherwise returns false.
检查 blade 视图中的角色:
@if(Auth::check())
@if(Auth::user()->hasRole('role-name') || Auth::user()->hasRoles('another-role-name'))
//Your markup here
@endif
@endif
对于多个角色:
@if(Auth::check())
@if(Auth::user()->hasAnyRoles(['role-name', 'another-role-name']))
//Your markup here
@endif
@endif
希望对您有所帮助。
我已经按照书上的规定为我的 laravel 程序设定了角色。问题出现在一个特定的实例中,用户模型中的一种方法总是 "pop-up" ... 无论用户对 role_id 有什么,方法总是显示特定角色的名称,在本例中为 "moderator"。不知道它有什么问题,或者至少我看不到它...
这是用户模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',"avatar"
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function rola(){
return $this->belongsTo("App\Roles", "id");
}
public function posts(){
return $this->hasMany("App\Post");
}
public function comments(){
return $this->hasMany("App\Comment");
}
public function isAdmin()
{//dd($user);
return $this->rola()->where('role', 'administrator');
}
public function isModerator()
{//dd($user);
return $this->rola()->where('role', 'moderator');
}
public function isUser()
{//dd($user);
return $this->rola()->where('role', 'user');
}
public function isPeon()
{//dd($user);
return $this->rola()->where('role', 'peon');
}
}
这是角色模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Roles extends Model
{
public function rola(){
return $this->hasMany("App\User", "role_id");
}
}
例如,如果只在任何视图中显示:
{{auth()->user()->rola->id}} <br> // 2 <- correct
{{auth()->user()->role_id}} // 3 <- this should be 2 also.
这是我的数据库模式的图像:
以及角色形象table:
默认情况下,每当新用户注册时,它都会自动接收 ID 为 4 的 "peon" 角色。 不确定我的错误在哪里,也许我没有看到什么......
编辑1:
这里有 link 到 github repo 供想看一看的人参考。
嗯,我认为问题出在 User.php 文件中的 rola()
函数:您正在为 belongsTo
这里的关系是 id
.
它应该是 role_id
因为这是键 Eloquent 会尝试映射到角色模型的 id
。 See the documentation 获取更多信息
问题出在您的 rola()
函数中。
您在 belongsTo
中使用了错误的外键。它应该是 role_id
,而不是 id
(目前是)。
这是一个经过一些改进的代码片段:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',"avatar"
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function rola(){
return $this->belongsTo("App\Roles", "role_id");
//Or just return $this->belongsTo(Roles::class);
}
public function posts(){
return $this->hasMany("App\Post");
}
public function comments(){
return $this->hasMany("App\Comment");
}
public function isAdmin()
{//dd($user);
return $this->hasRole('admin');
}
public function isModerator()
{//dd($user);
return $this->hasRole('moderator');
}
public function isUser()
{//dd($user);
return
return $this->hasRole('user');
}
public function isPeon()
{//dd($user);
return $this->hasRole('peon');
}
public function hasRole($role)
{
return $this->rola->role === $role;
}
}
我建议您将角色模型中的 rola
函数重命名为 users
,因为它 return 所有具有某种角色的用户。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Roles extends Model
{
public function users(){
return $this->hasMany(User::class, 'role_id');
}
}
还有一件事:看看this package。它确实可以帮助您处理用户、权限和权限组。
希望对您有所帮助。
要改进您的应用程序,请不要为您拥有的每个角色使用一个函数。
假设您在角色 table 上有 100 个角色,使用当前代码,您还需要 100 个函数,例如 isAdmin
或 isSomething
.
添加如下内容:
public function hasRole($roleName)
{
return $this->rola()->where('role', $roleName);
}
/**
* Check if the user has any of the given roles.
*/
public function hasAnyRole(array $roleNames)
{
$userRole = $this->rola;
return in_array($userRole->name, $roleNames);
}
现在您可以检查角色:
$user->hasRole('admin'); //If the user has the admin role, returns true. Otherwise returns false.
检查 blade 视图中的角色:
@if(Auth::check())
@if(Auth::user()->hasRole('role-name') || Auth::user()->hasRoles('another-role-name'))
//Your markup here
@endif
@endif
对于多个角色:
@if(Auth::check())
@if(Auth::user()->hasAnyRoles(['role-name', 'another-role-name']))
//Your markup here
@endif
@endif
希望对您有所帮助。