2017-01-12 55 views
0

我的產品型號多對多刪除樞使用標識Laravel 5.3

class Product extends Model 
{ 

public function transaction(){ 
     return $this->belongsToMany('App\Transaction', 'product_transaction', 'product_id', 'transaction_id') 
      ->withPivot('price', 'qty', 'discount_amt', 'product_total')->withTimestamps(); 
    } 

} 

我的交易模型

class Transaction extends Model 
{ 

    public function products(){ 

     return $this->belongsToMany('App\Product', 'product_transaction', 'transaction_id', 'product_id') 
      ->withPivot('price', 'qty', 'discount_amt', 'product_total')->withTimestamps(); 
    } 
} 

我的代碼刪除使用的product_id

public function deleteProducts($transId, $productId){ 

    $trans = Transaction::find($transId); 
    $trans->products()->detach($productId); 
} 

它的工作脫離... 問題是(見照片)我有一個重複的transaction_id和product_id w不同的數量。如果使用我的分離它刪除兩者。我正在考慮使用pivot ID。怎麼做。

enter image description here

回答