使用Pivot映射的Eloquent belongsToMany


Eloquent belongsToMany withPivot mapping

我继承了4个MySQL表:

列表
-id
-说明

位置
-id
-listing_id

指南
-id
-名称
……

指南位置
-id
-指南id
-location_id

列表=>(多个)位置
指南=>(多个)位置=>(1)列出

Listing、Location和Guide在Laravel Dingo应用程序中都是自己的模型。设置如下:

//Listing model
public function locations()
{
    return $this->hasMany('Location');
}

然后

//Location model
public function guides()
{
    return $this->belongsToMany('Guide');
}
public function listing()
{
    return $this->belongsTo('Listing');
}

//Guide model
public function locations()
{
    return $this->belongsToMany('Location');
}

我在guide_location中添加了一列,我想在从数据库中选择指南时返回它。

所以现在:

指南位置
-id
-指南id
-location_id
-位置

我尝试将->withPivot('position')添加到Guide和Location模型中的belongsToMany()语句中,但它不起作用,我看不到新的列。

你知道我做错了什么吗?

好的,->withPivot()正在工作,我只是没有看到结果,因为它在location.pivot.position中,而不是在location.position中。哑的

希望这能帮助其他人。