Hi,
Let’s say I have two models : Product and Invoice, both using the SoftDelete trait.
They are having a $belongsToMany relationship (xyz_plugin_product_invoice table with product_id and invoice_id).
Now, if I delete (softdelete) a Product, I need my pivot table to be updated as well.
I can’t let the pivot records pending in that table (this could cause an error if I try to access a Product attribute through an invoice),
I see two options :
Whenever a product or invoice is soft-deleted :
- the pivot table records are hard deleted.
- the pivot table records are soft-deleted.
Options 1 :
I could hook in the model BeforeDelete() function but I would prefer to keep it binded to the relationship.
Options 2 :
This won’t work :
'invoices' => [
'Invoice::class',
'table' => 'xyz_plugin_product_invoice',
'softDelete' => 'true'
]
So I tried to add a pivot model ProductInvoice, that use SoftDelete trait with $belongsTo invoice and product.
Then, on the Product model, I did this :
'invoices' => [
'Invoice::class',
'pivotModel' => [ProductInvoice::class, 'softDelete' => true, 'key' => 'product_id'],
]
But when I delete a Product, it’s not soft deleted.
Thanks for your suggestions/ideas.