我浪费了整整一天的时间,但无法弄清楚有多少关系。Laravel:hasManyThrough relationship
我有这样的关系:
# get related users
public function users()
{
return $this->hasManyThrough(User::class, FuneralHomeUser::class, 'user_id', 'id');
}
这产生了我这个查询:
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'
一切除了ON funeral_homes_users.id = users.id
好应该是ON funeral_homes_users.user_id = users.id
。所以我试图得到的唯一东西是,而不是id
,它应该有user_id
funeral_homes_users.id
(例如它应该是funeral_homes_users.user_id
),但我无法弄清楚。
下面是表以供参考:
// 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();
});
任何帮助将不胜感激。谢谢 !!!
你确定你有hasManyThrough并没有太多的一对多的关系? – naneri
@naneri:不知道,我是新来的,我已经发布了表格结构,如果是这种情况,最好有帮助。谢谢 – dev02
我怀疑这是hasManyThrough,因为'funeral_homes'与用户没有直接关系。 – dev02