Clean up unneeded records
PHP
Latest commit 6589954 Jan 2, 2017 @christophrumpel christophrumpel committed with freekmurze Update LICENSE.md (#7)
Update year of the license

README.md

Clean up unneeded records

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score Total Downloads

This package will clean up unneeded records for your Eloquent models.

The only things you have to do is let your models implement the GetsCleanedUp-interface and scheduled a command that performs the cleanup.

Here's a quick example of a model that implements GetsCleanedUp:

use Spatie\ModelCleanup\GetsCleanedUp;
use Illuminate\Database\Eloquent\Builder;
use Carbon\Carbon;

class LogItem extends Model implements GetsCleanedUp
{
    ...

     public static function cleanUp(Builder $query) : Builder
     {
        // Delete all records older than a year
        return $query->where('created_at', '<', Carbon::now()->subYear());
     }
}

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

You can install the package via composer:

composer require spatie/laravel-model-cleanup

Next up, the service provider must be registered:

'providers' => [
    ...
    Spatie\ModelCleanup\ModelCleanupServiceProvider::class,

];

Next, you must publish the config file:

php artisan vendor:publish --provider="Spatie\ModelCleanup\ModelCleanupServiceProvider"

This is the content of the published config file laravel-model-cleanup.php.

return [

    /*
     * All models that use the GetsCleanedUp interface in these directories will be cleaned.
     */
    'directories' => [
        // app_path('models'),
    ],

    /*
     * All models in this array that use the GetsCleanedUp interface will be cleaned.
     */
    'models' => [
        // App\LogItem::class,
    ],

];

Usage

All models that you want to clean up must implement the GetsCleanedUp-interface. In the required cleanUp-method you can specify a query that selects the records that should be deleted.

Let's say you have a model called NewsItem, that you would like to cleaned up. In this case your model could look like this:

use Spatie\ModelCleanup\GetsCleanedUp;
use Illuminate\Database\Eloquent\Builder;
use Carbon\Carbon;

class LogItem extends Model implements GetsCleanedUp
{
    ...

     public static function cleanUp(Builder $query) : Builder
     {
        return $query->where('created_at', '<', Carbon::now()->subYear());
     }

}

When running the console command clean:models all newsItems older than a year will be deleted.

This command can be scheduled in Laravel's console kernel.

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
   $schedule->command('clean:models')->daily();
}

Events

After the model has been cleaned Spatie\ModelCleanup\ModelWasCleanedUp will be fired (even if there were no records deleted). It has two public properties: modelClass and numberOfDeletedRecords.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.