如何从数据透视 table 中为 laravel 中的用户获取角色名称?
How to get the name of role from pivot table for a users in laravel?
Admin.php
[中间人]
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->role->name == 'admin') {
return $next($request);
}
return Redirect::route('home');
}
I want to check if the column name
of roles
table is equal to admin
.
I have tried with Auth::user()->role->name == 'admin')
.
我收到错误
Property [name] does not exist on this collection instance.
参考模型
User.php
[型号]
public function role()
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->withTimestamps();
}
参考table
users
table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->longText('cartitems')->nullable();
$table->longText('wishlist')->nullable();
$table->unsignedBigInteger('discount')->default(0);
$table->rememberToken();
$table->timestamps();
});
This is users
table. Notice there is no role directly here.
roles
table
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('display_name');
$table->timestamps();
});
This is roles
table. And every user have a role such as Superadmin
, admin
, seller
orcustomer
role_user
table
Schema::create('role_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('role_id');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->timestamps();
});
In this pivot table make a relationship between users
and roles
table.
这是因为根据您的架构设计,用户可以拥有多个角色。
用户 <=> 角色:多对多关系
所以Auth::user()->role
给出了角色模型实例的集合。而您正在尝试访问无法完成的集合中的名称 属性。
将 if 条件更改为此
if (Auth::check() && Auth::user->role()->where('name', 'admin')->exists()) {
我还建议您将用户模型中的关系名称从 role
更改为 roles
。因为 roles
更能提供有关用户和角色份额关系类型的信息。
public function roles(){
return $this->belongsToMany(App\Role::class, 'role_user' 'user_id', 'role_id');
}
那是因为您正在尝试访问 collection
上的值,而您的 laravel 关系是 BelongsToMany but rather HasOne/BelongsTo。您应该选择所有名称,然后检查它是否存在 in_array
$roles = Auth::check() ? Auth::user()->role->pluck('name')->toArray() : [];
if (in_array('admin', $roles)) {
return $next($request);
}
我建议将 role 关系更改为 roles,因为在这种情况下,一个用户可以拥有多个角色。
你可以使用whereHas
if (Auth::check() && User::where('id',Auth::user()->id)->whereHas('role',function ($query){
$query->where('roles.name','=','admin');
})->first()!=null ) {
return $next($request);
}
使用whereHas
$role = User::where('id',Auth::user()->id)->whereHas('role',function ($query){
$query->where('name','=','admin');
})->first();
if (Auth::check() && isset($role) && !empty($role) ) {
return $next($request);
}
Admin.php
[中间人]
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->role->name == 'admin') {
return $next($request);
}
return Redirect::route('home');
}
I want to check if the column
name
ofroles
table is equal toadmin
.
I have tried with
Auth::user()->role->name == 'admin')
.
我收到错误
Property [name] does not exist on this collection instance.
参考模型
User.php
[型号]
public function role()
{
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->withTimestamps();
}
参考table
users
table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->longText('cartitems')->nullable();
$table->longText('wishlist')->nullable();
$table->unsignedBigInteger('discount')->default(0);
$table->rememberToken();
$table->timestamps();
});
This is
users
table. Notice there is no role directly here.
roles
table
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('display_name');
$table->timestamps();
});
This is
roles
table. And every user have a role such asSuperadmin
,admin
,seller
orcustomer
role_user
table
Schema::create('role_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('role_id');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->timestamps();
});
In this pivot table make a relationship between
users
androles
table.
这是因为根据您的架构设计,用户可以拥有多个角色。
用户 <=> 角色:多对多关系
所以Auth::user()->role
给出了角色模型实例的集合。而您正在尝试访问无法完成的集合中的名称 属性。
将 if 条件更改为此
if (Auth::check() && Auth::user->role()->where('name', 'admin')->exists()) {
我还建议您将用户模型中的关系名称从 role
更改为 roles
。因为 roles
更能提供有关用户和角色份额关系类型的信息。
public function roles(){
return $this->belongsToMany(App\Role::class, 'role_user' 'user_id', 'role_id');
}
那是因为您正在尝试访问 collection
上的值,而您的 laravel 关系是 BelongsToMany but rather HasOne/BelongsTo。您应该选择所有名称,然后检查它是否存在 in_array
$roles = Auth::check() ? Auth::user()->role->pluck('name')->toArray() : [];
if (in_array('admin', $roles)) {
return $next($request);
}
我建议将 role 关系更改为 roles,因为在这种情况下,一个用户可以拥有多个角色。
你可以使用whereHas
if (Auth::check() && User::where('id',Auth::user()->id)->whereHas('role',function ($query){
$query->where('roles.name','=','admin');
})->first()!=null ) {
return $next($request);
}
使用whereHas
$role = User::where('id',Auth::user()->id)->whereHas('role',function ($query){
$query->where('name','=','admin');
})->first();
if (Auth::check() && isset($role) && !empty($role) ) {
return $next($request);
}