laravel关系很多很多

时间:2017-11-30 11:34:43

标签: php laravel

在Laravel,我和很多人有很多关系:

Invoice.php

class Invoice extends Model {
     public function items() {
       return $this->belongsToMany('App\Item', 'invoice_item', 'invoice_id', 
       'item_id')->withPivot('quantity'); 
}

Item.php

class Item extends Model {
 public function invoices() {
      return $this->belongsToMany('App\Invoice' ,'invoice_item', 'item_id', 'invoice_id');
   }
class Invoice_item extends Model
{
    protected $fillable = [
      'quantity',
    ];
}

listInvoice.blade.php

 @foreach($invoice->items as $item)
<tr>
  <td>{{ $item->pivot->quantity}}</td>
</tr>
  @endforeach 

问题是,当我尝试显示数量时,总会出现0 :(我哪里出错?

1 个答案:

答案 0 :(得分:0)

我假设你使用的是Laravel 5.5(因为没有提到)

Laerte所述,请使用 var x = new XMLHttpRequest(); x.withCredentials = true; x.open("GET", api_url, true); x.onload = function () { //alert(this.responseText); //alert(web_url); } x.onerror = function() { //alert( 'Error ' + this.status ); } x.send(null); 代替InvoiceItem

您的自定义中间模型必须从Invoice_item延伸;不是来自Pivot

Model