首页 > 解决方案 > 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
        }
      }
    ]
  }
]

如您所见,角色的权限为空。

标签: laraveleloquentrelationshipeager-loading

解决方案


我自己解决了。这是缓存模型的问题。刷新缓存php artisan cache:clear解决了它。


推荐阅读