laravel - Laravel 混合获取 Eloquent 急切加载嵌套多个模型
问题描述
场景是:我有一个用户模型、一个角色模型和一个具有多对多和一对多关系的权限模型。
用户模型(可以看到角色一对多关系,权限多对多关系):
<?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();
该关系并具有权限数据时。
我做错了什么?
我的数据库表是:用户、权限、角色、权限角色和权限用户。
编辑- 这是我从语句中得到的结果:
[
{
"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
}
}
]
}
]
如您所见,角色的权限为空。
解决方案
我自己解决了。这是缓存模型的问题。刷新缓存php artisan cache:clear
解决了它。
推荐阅读
- r - dplyr::mutate() return "Ops.factor(gene_id$symbol, x) 中的错误:因子的水平集不同"
- google-oauth - 在 google OAuth 中获取自定义用户字段
- javascript - 为什么我的表单不会向数据库提交用户信息?
- swift - 如何在 SwiftUI macOS 中创建第一响应者
- routing - 在Angular 9项目的页面之间导航时,我正在努力寻找关于滚动的解决方案
- node.js - 如何将 dom 操作脚本包含到 SSR Next.js 应用程序中
- function - 在 haskell 中优雅地定义许多函数值
- angular - 此构造函数与 Angular 依赖注入不兼容,除非它没问题
- r - Set.Seed 函数 - 我不明白为什么我们在训练数据中需要它
- android - expo 在每次保存时重新下载整个 javascript 包