SiavashBamshadnia / Laravel-Auto-Hard-Deleter by sbamtr

Laravel Auto Hard Deleter
179
44
4
Package Data
Maintainer Username: sbamtr
Maintainer Contact: sbamtr@gmail.com (Siavash Bamshadnia)
Package Create Date: 2020-02-17
Package Last Update: 2022-04-27
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-18 15:27:39
Package Statistics
Total Downloads: 179
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 44
Total Watchers: 4
Total Forks: 4
Total Open Issues: 0

Laravel Auto Hard Deleter

PHP Composer Build Status Build Status StyleCI Latest Stable Version Scrutinizer Code Quality Code Intelligence Status License

This package, deletes soft deleted rows automatically after a time interval that you define.

For Laravel 5.5+, 6 and Lumen

Installation

Step 1

Require the package with composer using the following command:

composer require sbamtr/laravel-auto-hard-deleter

Step 2

For Laravel

The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:

'providers' => [
    // ...
    \sbamtr\LaravelAutoHardDeleter\AutoHardDeleteServiceProvider::class,
];

For Lumen

Add this line of code under the Register Service Providers section of your bootstrap/app.php:

$app->register(\sbamtr\LaravelAutoHardDeleter\AutoHardDeleteServiceProvider::class);

Step 3

Now its the time for scheduling the command. in you app/Console/Kernel.php file, paste this code in schedule() function:

protected function schedule(Schedule $schedule)
{
    // ...
    $schedule->command(\sbamtr\LaravelAutoHardDeleter\HardDeleteExpiredCommand::class)->hourly();
    // ...
}

In the code above, the command scheduled to run hourly. you can change it. For more information, please read this page.

Step 4 (Optional)

You can publish the config file with this following command:

php artisan vendor:publish --provider="sbamtr\LaravelAutoHardDeleter\AutoHardDeleteServiceProvider" --tag=config

Note: If you are using Lumen, you have to use this package.

Also you can set the AUTO_HARD_DELETE_AFTER value in .env file. like the following code:

...
AUTO_HARD_DELETE_AFTER='1 day'
...

Usage

in your models that used SoftDeletes trait, you can enable Auto Hard Delete with this line:

class SampleModel extends Model
{
    use SoftDeletes;
    const AUTO_HARD_DELETE_ENABLED = true;
}

Just write const AUTO_HARD_DELETE_ENABLED = true in your models! Also you can set expiration time for your deleted entities using the following line:

const AUTO_HARD_DELETE_AFTER = '5 months';

In the code above, expiration time for your soft deleted entity model is 5 months. More complete code is:

class SampleModel extends Model
{
    use SoftDeletes;
    const AUTO_HARD_DELETE_ENABLED = true;
    const AUTO_HARD_DELETE_AFTER = '5 months';
}

You can set any other values for AUTO_HARD_DELETE_AFTER like 5(means 5 days), 2 hours, 45 days, 2.5 months, 1 year, etc.

Note: If you don't set any value for AUTO_HARD_DELETE_AFTER in your model, the soft deleted models with AUTO_HARD_DELETE_ENABLED = true will be hard deleted after the time defined in config file named auto-hard-deleter.php.

Auto Hard Delete Command

Also you can hard delete expired rows manually using this artisan command:

php artisan hard-delete-expired

Writen with ♥ by Siavash Bamshadnia.

Please support me by staring this repository.