Skip to content
Advertisement

restore soft delete child in laravel

I am using laravel and I want to restore child when I restore parent for example I’ve removed a user and every user has a shop so shop was deleted with $cascadeDeletes and all child related to user but when I want to restore user her/his shop won’t restore

here is Shop Model

  protected $cascadeDeletes = ['ProductCategories' , 'products', 'slideshows', 'purchases', 'vouchers', 'brands', 'feedbacks', 'tags', 'subscribers', 'invoice', 'donwloadLinkRequests', 'stats', 'compares', 'wishlists', 'faqs','shopContact']

and User Model

  protected $cascadeDeletes = ['addresses', 'userInformation', 'wishlist', 'comments','compare', 'shop', 'purchases', 'cart'];

** I am using iatstuti/laravel-cascade-soft-deletes so is there any way with this**

Advertisement

Answer

You can add a listener for the user models restoring event and restore the related shop. In the user models boot function, add the following:

static::restoring(function($user) {
    $user->shop()->withTrashed()->first()->restore();
});

See model events.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement