Soft Delete Cascade using Laravel Eloquent Example

Today, We want to share with you Soft Delete Cascade using Laravel Eloquent Example.In this post we will show you Laravel Soft Delete Cascade, hear for Cascading Soft Deletes with Laravel 5.7 we will give you demo and example for implement.In this post, we will learn about Laravel – Eloquent: Cascading delete, forceDelete and restore with an example.

Soft Delete Cascade using Laravel Eloquent Example

There are the Following The simple About Soft Delete Cascade using Laravel Eloquent Example Full Information With Example and source code.

Also Read This πŸ‘‰   Laravel cookie – Get, Set, Delete Cookies

As I will cover this Post with live Working example to develop Soft delete cascade laravel, so the Soft Deleting Parent & Child Models in Eloquent Laravel 5.7 for this example is following below.

using Laravel Composer

$ composer require iatstuti/laravel-cascade-soft-deletes="1.0.*"

Cascading soft deletes with Eloquent

class Article extends Model
{
    use SoftDeletes, CascadeSoftDeletes;

    protected $cascadeDeletes = ['opinions'];

    protected $dates = ['deleted_at'];

    public function opinions()
    {
        return $this->hasMany(Opinions::class);
    }
}    

Laravel Destroy Methods

$article = App\Article::find($articleId)
$article->delete(); 

Laravel Soft delete the article, which will also trigger the delete() method on any Opinions and their children.

Also Read This πŸ‘‰   Laravel check if Ajax request Example

Laravel CascadeSoftDeletes

composer require exls/laravel-soft-deletes

Laravel 5.* Integration

config/app.php

  'providers'  => array(

      //register listeners on events
      Exls\LaravelCascadeSoftDeletes\Providers\CascadeSoftDeletesServiceProvider::class,

  ),

Laravel Model With Controller

<?php

namespace App\Models;

use App\Models\Article\Detail;
use Exls\LaravelCascadeSoftDeletes\Traits\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //Instead of SoftDeletes
    use CascadeSoftDeletes;

    //Remove immideately comments
    protected $cascadeDeletes = ['comments'];
    
    // or use queues to soft delete comments
    protected $queuedCascadeDeletes = ['comments'];

  protected $dates = ['deleted_at'];

    public function comments()
    {
        return $this->hasMany(Detail::class);
    }
}   

//Laravel Destroy Methods
App\Models\Article::findOrFail($articleId)->delete()

Angular 6 CRUD Operations Application Tutorials

Read :

Free Download Example – Pakainfo.com

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

Also Read This πŸ‘‰   Laravel 6 Delete and soft delete Examples

I hope you get an idea about Soft Delete Cascade using Laravel Eloquent Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.