【发布时间】:2019-08-02 23:31:59
【问题描述】:
我有两个以多对多关系相互关联的表。我已经在双方都使用方法 $this->belongsToMany() 建立了查询。
一个表叫做Device,另一个叫做Pos。我想使用 Eloquent 在中间表中访问与该 Pos 相关的所有设备实例。
这是设备型号:
public function pos()
{
return $this->belongsToMany('App\Pos', 'devices_pos')->withTimestamps();
}
这是 Pos 模型:
public function devices()
{
return $this->belongsToMany('App\Device', 'devices_pos')->withTimestamps();
}
我不想直接对“devices_pos”表进行数据库查询。我想用 Eloquent 的方式来做。
【问题讨论】:
-
要访问中间表吗?看这里:laravel.com/docs/5.8/…