2017-06-08 117 views
0

我正在創建一個表格,顯示請求列表,我在ID的'erp_createdid'和'erp_productid'之間有關係。爲了顯示產品的名稱,我創建了一個belongsToMany,其中我指定了我的兩個'ID's'。BelongToMany的Laravel 5.4不起作用

訂貨型號

class Order extends Model 
{ 
    protected $table = 'erp_order'; 
    protected $fillable = ['erp_createdid']; 
    protected $primaryKey = 'erp_orderid'; 

    public function description() 
    { 
     return $this->belongsToMany('App\Description','erp_order_product', 'erp_createdid', 'erp_productid'); 
    } 

} 

描述型號

class Description extends Model 
{ 
    protected $table = 'erp_product_description'; 
    protected $primaryKey = 'erp_productid'; 
    protected $fillable = ['erp_name']; 
    public $timestamps = false; 

    public function order() 
     { 
      return $this->belongsToMany('App\Order', 'erp_order_product', 'erp_createdid', 'erp_productid'); 
     } 

<td>@foreach($orders->description as $des) 
{{$des->erp_name}} 
@endforeach 
</td> 

但在價值觀表不顯示,有什麼建議?

enter image description here

enter image description here

enter image description here

回答

0

我覺得你有OrderbelongsToMany參數不正確的順序。

試試這個:

public function description() 
{ 
    return $this->belongsToMany('App\Description','erp_order_product', 'erp_productid', 'erp_createdid'); 
} 

也可以嘗試更新您的foreach這樣的:

哦OK,試試這個:

@foreach($orders as $order) 
    @foreach($order->description as $description) 
     {{ $description->erp_name }} 
    @endforeach 
@endforeach 
+0

沒有工作,同樣的事情。 –

+0

然後你可以顯示更多的代碼嗎? '$ orders'是什麼? – Jan

+0

$ order = Order :: orderBy('erp_orderid','desc') - > paginate(20); –