【发布时间】:2019-10-04 11:34:04
【问题描述】:
我的表结构与 laravel 文档中提到的 HasManyThrough 关系 hasManyThrough 中的表结构相同
countries
id - integer
name - string
users
id - integer
country_id - integer
name - string
posts
id - integer
user_id - integer
title - string
并定义与文档中相同的关系。
public function posts()
{
return $this->hasManyThrough(
'App\Post', 'App\User',
'country_id', 'user_id', 'id'
);
}
现在当我列出特定国家的帖子时。我也需要帖子用户的信息。我的意思是来自数据透视表(用户)的信息
$posts = Country::find(2)->posts();
以上仅返回帖子数据..
【问题讨论】:
标签: laravel relationship has-many-through