场景是:我有一个用户模型、一个角色模型和一个权限模型,具有多到多、一到多的关系。
用户模型(您可以看到角色一到多关系,权限多到多关系):
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
// ------------------------------------------------------------------------------
// Model Traits
// ------------------------------------------------------------------------------
use Notifiable;
// ------------------------------------------------------------------------------
// Model configuration (extended from parent)
// ------------------------------------------------------------------------------
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token'
];
/**
* The attributes that should be eager loaded.
*
* @var array
*/
protected $with = ['role', 'permissions'];
// ------------------------------------------------------------------------------
// Model Relationships
// ------------------------------------------------------------------------------
public function role() {
return $this->belongsTo('App\Models\Role');
}
public function permissions() {
return $this->belongsToMany('App\Models\Permission');
}
}榜样:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
class Role extends Model
{
use Cachable;
protected $with = ['permissions'];
// ------------------------------------------------------------------------------
// Model Relationships
// ------------------------------------------------------------------------------
public function users() {
return $this->hasMany('App\Models\User');
}
public function permissions() {
return $this->belongsToMany('App\Models\Permission');
}
}权限模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
class Permission extends Model
{
use Cachable;
// ------------------------------------------------------------------------------
// Model configuration (extended from parent)
// ------------------------------------------------------------------------------
/**
* Campos autorellenables para la generación automática de modelos
*/
protected $fillable = ['name'];
// ------------------------------------------------------------------------------
// Model Relationships
// ------------------------------------------------------------------------------
public function users() {
return $this->belongsToMany('App\Models\User');
}
public function roles() {
return $this->belongsToMany('App\Models\Permission');
}
}如果我调用User::with(['role', 'permissions'])->get()甚至User::with(['role.permissions', 'permissions'])->get(),就会得到用户数据、角色数据和权限数据。一切都很好,除了角色权限从未加载。因此,如果不为这个用户角色权限添加另一个查询,我就无法使用它们。
我知道角色具有权限,因为当我执行Role::where('id', 3)->with('permissions')->get();时,关系可以工作,并且具有权限数据。
我做错什么了?
我的DB表是:用户、权限、角色、permission_role和permission_user。
编辑--这是我从语句中得到的结果:
[
{
"id": 1,
"role_id": 3,
"name": "UserName",
"email": "username@domain.com",
"email_verified_at": null,
"created_at": "2018-12-10 13:04:00",
"updated_at": "2018-12-10 13:04:00",
"role": {
"id": 3,
"name": "Editor",
"created_at": "2018-12-10 13:03:31",
"updated_at": "2018-12-10 13:03:31",
"permissions": []
},
"permissions": [
{
"id": 3,
"name": "Delete post",
"created_at": "2017-10-23 11:07:43",
"updated_at": "2017-10-23 11:07:43",
"pivot": {
"user_id": 1,
"permission_id": 3
}
}
]
}
]如您所见,角色的权限为空。
发布于 2018-12-19 10:51:12
我自己解决的。这是缓存模型的一个问题。刷新缓存php artisan cache:clear正确地解决了这个问题。
https://stackoverflow.com/questions/53849116
复制相似问题