【发布时间】:2018-03-08 01:31:27
【问题描述】:
我最近开始学习 Laravel 5.4,我的 3 向表关系有问题,我在网上看了一些关于多对多的文章,但这种关系只是双方的“hasOne”。
谁能给我一个关于如何构建我的表关系的有用提示,这里是 PK/FK 关系:
Users table (id)
Listings table (id, user_id)
Insights table (id, listing_id) - one insight row per listing only.
以及以下型号:
用户模型
class User extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
上市模式
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
}
洞察模型
class Insight extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
我想要实现的是查询用户自己的列表,每个列表都是当前的见解。
非常感谢。
西蒙。
【问题讨论】:
-
你的意思是,1 个用户有 1 个列表,其中有 1 个见解?
-
嗨@aleksandrs,谢谢,用户可以有很多列表,但列表只有一个见解