简体   繁体   中英

Delete all related id from multiple tables in Laravel 5.2

How can I delete related id's from multiple tables.

For example:

I have an invoice table which stores all the headers and an invoice body table which stores all the details of the invoice and invoice footer which stores all the amounts of the invoice, and I want to delete my main part is to delete the main invoice table and all its related id's form multiple table.

How to do that?

You could do it in the AppServiceProvider:

Invoice::deleting(function($invoice) {
    $invoice->footer()->delete();
    $invoice->body()->delete();
});

If the invoice is deleted, the event "deleting" will be fired and the related models will be deleted too.

You can write this

Invoice::deleting(function($invoice) {
    $invoice->footer()->delete();
    $invoice->body()->delete();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM