PHP code example of gigerit / laravel-cascade-delete

1. Go to this page and download the library: Download gigerit/laravel-cascade-delete library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gigerit / laravel-cascade-delete example snippets


namespace App\Models;

use Gigerit\LaravelCascadeDelete\Concerns\CascadeDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
    use CascadeDeletes;

    protected $cascadeDeletes = [
        'posts',      // HasMany
        'profile',    // HasOne
        'roles',      // BelongsToMany (will detach)
        'comments',   // MorphMany
    ];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function profile()
    {
        return $this->hasOne(Profile::class);
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

(new User)->clearOrphanMorphRelations();

return [
    'models_paths' => [
        app_path(),
    ],

    'models_excluded_directories' => [
        'Tests',
        'tests',
    ],
];
bash
php artisan cascade-delete:clean
bash
php artisan cascade-delete:clean --dry-run
bash
php artisan vendor:publish --tag="cascade-delete-config"