Laravel 混合获取 Eloquent 急切加载嵌套多个模型
Posted
技术标签:
【中文标题】Laravel 混合获取 Eloquent 急切加载嵌套多个模型【英文标题】:Laravel mixed get Eloquent eager loading nested with multiple models 【发布时间】:2019-05-19 19:41:39 【问题描述】:场景是:我有一个用户模型、一个角色模型和一个具有多对多和一对多关系的权限模型。
用户模型(可以看到角色一对多关系,权限多对多关系):
<?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();
时,该关系有效并且具有权限数据。
我做错了什么?
我的数据库表是:users、permissions、roles、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
]
]
如您所见,该角色的权限为空。
【问题讨论】:
您是否需要一个“hasManyThrough”关系?一个用户通过一个角色有很多权限? 【参考方案1】:我自己解决了。这是缓存模型的问题。刷新缓存php artisan cache:clear
解决了。
【讨论】:
以上是关于Laravel 混合获取 Eloquent 急切加载嵌套多个模型的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 在急切加载时使用 Eloquent 选择特定列
laravel eloquent - 在嵌套急切加载的关系上不使用