在pivot Laravel 5.3中使用Id进行多对多删除
我的产品模型在pivot Laravel 5.3中使用Id进行多对多删除,laravel,Laravel,我的产品模型 class Product extends Model { public function transaction(){ return $this->belongsToMany('App\Transaction', 'product_transaction', 'product_id', 'transaction_id') ->withPivot('price', 'qty', 'discount_amt', 'product_
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();
}
}
要使用产品标识删除的我的代码
public function deleteProducts( $transId, $productId ){
$trans = Transaction::find( $transId);
$trans->products()->detach( $productId );
}
它的工作是分离。。。
问题是看照片,我有一个重复的交易id和产品id,数量不同。如果使用“我的分离”,则会同时删除这两者。我正在考虑使用pivot id。如何使用
我能用计算机解决这个问题
$trans = Transaction::find( $transId);
$trans->products()->newPivotStatementForId( $productId )->where('id', $pivotId)->delete();
多亏了这个网站