[英]How to get foreign key name of loaded relationship using laravel eloquent
So I currently have 3 models Specie
Type
User
. 所以我目前有3种Specie
Type
User
模型。 I want to be able to get the name of the last modified user in relation to the Specie
model 我希望能够获得有关Specie
模型的最后修改用户的名称
The relationships are as follows 关系如下
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');
}
}
I have tried this 我已经试过了
Class Specie extends Model {
public function lastModified()
{
return $this->belongsTo('App\User', 'last_modified_by');
}
}
And then used the below code 然后用下面的代码
$this->type->with(['specie.lastModified' => function ($query) use
($userId) {
$query->where('user_id', $userId);
}])->get();
Unfortunately this does not seem to work 不幸的是,这似乎不起作用
However, by using this code 但是,通过使用此代码
$this->type->with(['specie' => function ($query) use ($userId) {
$query->where('user_id', $userId);
}])->get();
I am able to get this: 我能够得到这个:
"id": 1,
"type": "Halo",
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
{
"id": 5,
"user_id": 1,
"type_id": 1,
"note": "et",
"created_by": 1,
"last_modified_by": 1,
}
]
However, what I want to get is the name of the last modified person name which is the primary key in the User
model and the foreign key in the Specie
model 但是,我要获取的是最后修改的人的姓名,这是User
模型中的主键,而Specie
模型中的外键
This is what I expect to get: 这是我期望得到的:
"id": 1,
"type": "Halo",
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
{
"id": 5,
"user_id": 1,
"type_id": 1,
"note": "et",
"created_by": 1,
"last_modified_by": 1,
"last_modified_name": 'gerrard'
}
]
in your model like this 在这样的模型中
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class lastModified extends Model
{
protected $table = 'lastModifieds';
}
From your Eloquent entity relationships: 从您雄辩的实体关系中:
Specie
and Type(type)
are related (one-to-many) Specie
和Type(type)
是相关的(一到多)
Specie
and User(user)
are related (one-to-many) Specie
和User(user)
相关(一对多)
Specie
and User(lastModified)
are related (one-to-many) Specie
和User(lastModified)
相关(一对多)
User
and Type
are not related User
和Type
不相关
So, for you to include the user name you have to query the Specie
model through the lastModified()
method. 因此,要包括用户名,您必须通过lastModified()
方法查询Specie
模型。
$this->lastModified()->with(['specie' => function ($query) use ($userId) {
$query->where('user_id', $userId);
}])->get();
You should expect a result like this 您应该期望这样的结果
"id": 1,
"name": "the_user_name"
// any other user data
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
{
"id": 5,
"user_id": 1,
"type_id": 1,
"note": "et",
"created_by": 1,
"last_modified_by": 1,
}
]
From the result, the field(foreign key) last_modified_by
in specie
with value 1
should match the field id
in user
. 从结果来看, specie
的字段(外键) last_modified_by
值为1
应该与user
中的字段id
匹配。
Also, I'm assuming the field for your user name is name
. 另外,我假设您的用户名字段为name
。 It would be included with the returned user data. 它会包含在返回的用户数据中。
UPDATE 更新
You can not get any user data from the User
table through the Type
table using eager loading except they are related. 您不能使用急切的加载通过Type
表从User
表中获取任何用户数据,除非它们是相关的。 Ergo, models must be related (ie constrained by each other) before they can be nested, for instance using eager loading. 因此,必须先关联(即相互约束)模型,然后才能嵌套它们,例如使用急切的加载。 See this medium article to better understand this. 请参阅本文,以更好地理解这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.