dongyi9023 2016-05-13 18:06 采纳率: 0%
浏览 60
已采纳

Eloquent属于ToMany withPivot mapping

I've inherited 4 MySQL tables:

listings
-id
-description
...

locations
-id
-listing_id

guides
-id
-name
....

guide_location
-id
-guide_id
-location_id

Listing=>(many) Locations
Guide=>(many) Locations=>(1) Listing

Listing, Location and Guide each is its own model in a Laravel Dingo application. Here's the setup:

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

Then

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

And

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

I've added a column to guide_location and I want to return it when I select a Guide from the database.

So now:

guide_location
-id
-guide_id
-location_id
-position

I tried adding ->withPivot('position') to both belongsToMany() statements in the Guide and Location models but it's not working, I'm not seeing the new column.

Any ideas what I'm doing wrong?

  • 写回答

1条回答 默认 最新

  • doulue7522 2016-05-13 19:32
    关注

    OK, ->withPivot() was working, I just was not seeing the result because it was in location.pivot.position, not location.position. Dumb.

    Hope this helps others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题