【问题标题】:How would I be able to get the foreign key name in another table using nested eager loading with eloquent and laravel如何使用带有 eloquent 和 laravel 的嵌套急切加载在另一个表中获取外键名称
【发布时间】:2019-09-01 01:30:41
【问题描述】:

所以我目前有3个模型SpecieTypeUser。我希望能够获得与 Specie 模型相关的最后修改用户的名称

关系如下

Class Specie extends Model {
   public function type()
   {
        return $this->belongsTo('App\Type', 'type_id');
   }

   public function user()
   {
        return $this->belongsTo('App\User', 'user_id');
   }
}

Class Type extends Model {
   public function specie()
   {
        return $this->hasMany('App\Specie', 'type_id');
   }
}

Class User extends Model {
   public function specie()
   {
        return $this->hasMany('App\Specie', 'user_id');
   }
}

我试过了

Class Specie extends Model {
    public function lastModified()
    {
        return $this->belongsTo('App\User', 'last_modified_by');
    }
}

然后使用下面的代码

$this->type->with(['specie.lastModified' => function ($query) use 
        ($userId) {
            $query->where('user_id', $userId);
        }])->get();

不幸的是,这似乎不起作用

但是,通过使用此代码

$this->type->with(['specie' => function ($query) use ($userId) {
            $query->where('user_id', $userId);
        }])->get();

我能得到这个:

"id": 1,
"type": "Halo",
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
  {
    "id": 6,
    "user_id": 1,
    "type_id": 1,
    "note": "et",
    "last_modified_by": 1,
  },
  {
    "id": 7,
    "user_id": 1,
    "type_id": 2,
    "note": "sa",
    "last_modified_by": 2,
  },
]

但是,我想得到的是最后修改的人名的名字,它是 User 模型中的主键和 Specie 模型中的外键

这是我期望得到的:

"id": 1,
"type": "Halo",
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
  {
    "id": 6,
    "user_id": 1,
    "type_id": 1,
    "note": "et",
    "last_modified_by": 1,
    "user": [
        {
         "id": 1,
         "user": 'gerrard'
        }
    ]
  },
  {
    "id": 7,
    "user_id": 1,
    "type_id": 2,
    "note": "sa",
    "last_modified_by": 2,
    "user": [
         {
           "id": 2,
           "user": 'chris'
         }
    ]
  }
]

【问题讨论】:

    标签: php mysql laravel laravel-5 eloquent


    【解决方案1】:

    with() 内的 with() 应该这样做:

    $this->type->with(['specie' => function ($query) use ($userId) {
        $query->where('user_id', $userId);
        $query->with('lastModified');
    }])->get();
    

    唯一的区别是关系键不是命名为"user",而是关系的名称("last_modified")。

    "id": 1,
    "type": "Halo",
    "created_at": "2019-07-20 13:02:53",
    "updated_at": "2019-07-20 13:02:53",
    "specie": [
        {
            "id": 6,
            "user_id": 1,
            "type_id": 1,
            "last_modified_by": 2,
            "note": "et",
            "last_modified": {
                "id": 2
            }
        }
    ]
    

    【讨论】:

    • 如何获取 lastModified 的特定列。 $this->type->with(['specie' => function ($query) use ($userId) { $query->where('user_id', $userId); $query->with('lastModified')->select('id', 'name'); }])->get();
    • 最简单的方法是$query->with('lastModified:id,name');。另一种方法是使第二个with() 成为嵌套函数,并在其中放置一个select(),
    猜你喜欢
    • 2019-05-19
    • 2017-09-30
    • 2023-03-20
    • 2020-01-04
    • 2020-05-28
    • 2015-08-16
    • 2015-11-17
    • 1970-01-01
    相关资源
    最近更新 更多