PHP code example of spatie / laravel-model-cleanup

1. Go to this page and download the library: Download spatie/laravel-model-cleanup 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/ */

    

spatie / laravel-model-cleanup example snippets


use Illuminate\Database\Eloquent\Model;
use Spatie\ModelCleanup\CleanupConfig;
use Spatie\ModelCleanup\GetsCleanedUp;

class YourModel extends Model implements GetsCleanedUp
{
    ...
    
     public function cleanUp(CleanupConfig $config): void
     {
         $config->olderThanDays(5);
     }
}

return [

    /*
     * All models in this array that implement `Spatie\ModelCleanup\GetsCleanedUp`
     * will be cleaned.
     */
    'models' => [
        // App\Models\YourModel::class,
    ],
];


// in app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command(\Spatie\ModelCleanup\Commands\CleanUpModelsCommand::class)->daily();
}

use Illuminate\Database\Eloquent\Model;
use Spatie\ModelCleanup\CleanupConfig;
use Spatie\ModelCleanup\GetsCleanedUp;

class YourModel extends Model implements GetsCleanedUp
{
    ...
    
     public function cleanUp(CleanupConfig $config): void
     {
        $config->olderThanDays(5);
     }
}

// in config/model-cleanup.php

return [
    'models' => [
        App\Models\YourModel::class,
    ],
    
    // ...
]

 public function cleanUp(CleanupConfig $config): void
 {
    $config->olderThanDays(5);
 }

 public function cleanUp(CleanupConfig $config): void
 {
    $config->olderThan(now()->subYear());
 }

 public function cleanUp(CleanupConfig $config): void
 {
    $config
        ->olderThanDays(5)
        ->useDateAttribute('custom_date_column');
 }

 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->scope(fn (Illuminate\Database\Eloquent\Builder $query) => $query->where('status', 'inactive'));
}

 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->chunk(1000);
}

 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->chunk(1000, fn() => YourModel::count() > 5000);
}
bash
php artisan vendor:publish --provider="Spatie\ModelCleanup\ModelCleanupServiceProvider"