如何访问 Laravel 5 数据透视表中的额外字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29270139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:13:30  来源:igfitidea点击:

How do I access extra fields in a Laravel 5 pivot table?

laravelmodelpivot-tablelaravel-5

提问by Dylan Glockler

I have two Laravel 5 models connected by a pivot table and belongsToMany relationships. As you can see below I have orders, items and the pivot table item_order.

我有两个 Laravel 5 模型,由一个数据透视表和belongsToMany 关系连接。正如您在下面看到的,我有订单、项目和数据透视表 item_order。

Order:

命令:

public function items() {
    return $this -> belongsToMany('App\Item', 'item_order', 'order_id', 'item_id') -> withPivot('id','quantity');
}

Item:

物品:

public function orders() {
    return $this -> belongsToMany('App\Order', 'item_order', 'item_id', 'order_id') -> withPivot('id','quantity');
}

When looping through

循环时

$orders->items as $item

I can't seem to access the extra field 'quantity'. If I

我似乎无法访问额外的字段“数量”。如果我

dd($item) 

I get:

我得到:

Item {#310 ▼
  #table: "items"
  +timestamps: true
  #dates: array:1 [?]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  #attributes: array:11 [▼
    "id" => 1
    "created_at" => "2015-03-23 21:30:19"
    "updated_at" => "2015-03-25 15:37:23"
    "deleted_at" => null
    "name" => "Medium Lift Sling"
    "description" => "Green with orange details"
    "url" => "http://www.atlaslifttech.com/slings/patienthighback"
    "image" => "atlas-highback-sling.jpg"
    "manufacturer_id" => 1
    "itemtype_id" => 1
    "notes" => null
  ]
  #original: array:15 [▼
    "id" => 1
    "created_at" => "2015-03-23 21:30:19"
    "updated_at" => "2015-03-25 15:37:23"
    "deleted_at" => null
    "name" => "Medium Lift Sling"
    "description" => "Green with orange details"
    "url" => "http://www.atlaslifttech.com/slings/patienthighback"
    "image" => "atlas-highback-sling.jpg"
    "manufacturer_id" => 1
    "itemtype_id" => 1
    "notes" => null
    "pivot_order_id" => 1
    "pivot_item_id" => 1
    "pivot_id" => 6
    "pivot_quantity" => 0
  ]
  #relations: array:2 [?]
  #hidden: []
  #visible: []
  #appends: []
  #fillable: []
  #guarded: array:1 [?]
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  #forceDeleting: false
}

So the quantity is there under original but I've tried

所以数量在原件下,但我试过了

{{ $item->quanity }} and {{ $item->pivot_quantity }}

and even

乃至

{{ $item->original['pivot_quantity'] }}

And nothing is ever output.

并且没有任何输出。

回答by Joseph Silber

You access the properties on the pivot property:

您可以访问枢轴属性上的属性:

$item->pivot->quantity


P.S.In the future, instead of dding the model directly, convert it to an array first:

PS以后不要dd直接 ing 模型,先把它转成数组:

dd($item->toArray());

It'll be easier to see just the attributes and relations.

仅查看属性和关系会更容易。