[英]Laravel: hasManyThrough relationship
I wasted my whole day but cannot figure out hasManyThrough relationship. 我浪费了整整一天,但无法确定有很多联系。
I have this relationship: 我有这种关系:
# get related users
public function users()
{
return $this->hasManyThrough(User::class, FuneralHomeUser::class, 'user_id', 'id');
}
This generates me this query: 这将生成以下查询:
SELECT `users`.*, `funeral_homes_users`.`user_id`
FROM `users`
INNER JOIN `funeral_homes_users` ON `funeral_homes_users`.`id` = `users`.`id`
WHERE `funeral_homes_users`.`user_id` = '4'
Everything is good except for ON funeral_homes_users.id = users.id
should be ON funeral_homes_users.user_id = users.id
. 一切都很好,除了
ON funeral_homes_users.id = users.id
应该是ON funeral_homes_users.user_id = users.id
。 So the only thing I am trying to get is instead of id
, it should have user_id
for funeral_homes_users.id
(eg it should be funeral_homes_users.user_id
) but I am unable to figure it out. 因此,我唯一想要获取的是代替
id
,它应该具有用于funeral_homes_users.id
user_id
(例如,应该是funeral_homes_users.user_id
),但是我无法弄清楚。
Here are tables for reference: 以下是供参考的表格:
// funeral_homes
Schema::create('funeral_homes', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 255);
$table->timestamps();
});
// funeral_homes_users
Schema::create('funeral_homes_users', function (Blueprint $table) {
$table->increments('id');
$table->integer('funeral_home_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
});
// users
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->string('first_name');
$table->string('last_name');
$table->timestamps();
});
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks !!!
谢谢 !!!
If I understand it correctly, your case is: 如果我正确理解,您的情况是:
USER
has many FUNERAL_HOME
USER
有很多FUNERAL_HOME
FUNERAL_HOME
belongs to many USER
FUNERAL_HOME
属于许多USER
if that is correct, your relation methods should return something like this: 如果正确,则您的关系方法应返回以下内容:
User.php
public function FuneralHomes()
{
return $this->belongsToMany(FuneralHome::class, 'funeral_homes_users');
}
FuneralHome.php
public function Users()
{
return $this->belongsToMany(User::class, 'funeral_homes_users');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.