laravel-model-cleanup

清理不需要的记录。「Clean up unneeded records」

Github星跟蹤圖

Clean up unneeded records

Latest Version on Packagist
MIT Licensed
Build Status
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 schedule 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());
     }
}

Installation

You can install the package via composer:

composer require spatie/laravel-model-cleanup

Next, you must publish the config file:

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

This is the content of the published config file 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,
    ],

    /*
     * Specify whether to search the configured `directories` recursively. 
     * Set to false to only search for models directly inside the specified paths.
     */
    'recursive' => true,
];

Usage

Configure models to remove

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 LogItem, 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 logItems older than a year will be deleted.

Configure models to forceRemove

All models that have the SoftDeletes trait that you want to clean up completly from the database must implement the GetsForcedCleanedUp-interface. In the required
forceCleanUp-method you can specify a query that selects the records that should be forceDeleted.

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

use Spatie\ModelCleanup\GetsForcedCleanedUp;
use Illuminate\Database\Eloquent\Builder;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;

class LogItem extends Model implements GetsForcedCleanedUp
{
    use SoftDeletes;
    ...
    
    public static function forceCleanUp(Builder $query) : Builder
    {
        return $query->onlyTrashed()->where('deleted_at', '<', Carbon::now()->subDay());
    }
    
}

When running the console command clean:models all logItems that were deleted more than a year from Carbon::now will be deleted completly from the database.

Command

When running the console command clean:models all the items on cleanUp 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 on 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.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

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

We publish all received postcards on our company website.

Credits

Support us

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

Does your business depend on our contributions? Reach out and support us on Patreon.
All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

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

主要指標

概覽
名稱與所有者spatie/laravel-model-cleanup
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2016-02-24 11:38:27
推送於2021-08-02 19:46:09
最后一次提交2021-08-02 21:46:09
發布數26
最新版本名稱3.2.0 (發布於 )
第一版名稱0.0.1 (發布於 )
用户参与
星數396
關注者數11
派生數43
提交數169
已啟用問題?
問題數13
打開的問題數0
拉請求數26
打開的拉請求數0
關閉的拉請求數7
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?