How to soft delete related records when soft deleting a parent record in Laravel?

前端 未结 4 596
无人及你
无人及你 2021-02-04 01:36

I have this invoices table that which has the following structure

id | name | amount | deleted_at
2    iMac   1500   | NULL

and a payments tabl

4条回答
  •  深忆病人
    2021-02-04 02:13

    I know you asked this question a long time ago but I found this package to be very simple and straightforward.

    Or you can use this package it's useful too.

    Remember to install the right version depending on your laravel version.

    You must install it via composer:

     composer require askedio/laravel5-soft-cascade ^version
    

    In second package:

     composer require iatstuti/laravel-cascade-soft-deletes
    

    Register the service provider in your config/app.php.

    you can read the docs on the GitHub page.

    If you delete a record this package recognizes all of its children and soft-delete them as well.

    If you have another relationship in your child model use its trait in that model as well. its so much easier than doing it manually.

    The second package has the benefit of deleting grandchildren of the model. in some cases, I say its a better approach.

提交回复
热议问题