paulund / eloquent-lifetime
Laravel package to delete records after a certain amount of time
Fund package maintenance!
paulund
Requires
- php: ^8.3
- illuminate/contracts: ^11.0
- illuminate/support: ^11.0
Requires (Dev)
- larastan/larastan: ^2.0
- laravel/pint: ^1.0
- orchestra/testbench: ^9.4
- pestphp/pest: ^3.0
- rector/rector: ^1.2
README
This is a Laravel package that allows you to set a lifetime on an Eloquent model.
The package includes a trait that you can add to your model to set a lifetime on the model.
There is also a command that will run in the background to automatically delete the model after the lifetime has expired.
Installation
Install the package via composer:
composer require paulund/eloquent-lifetime
Publish the configuration file:
php artisan vendor:publish --provider="Paulund\EloquentLifetime\EloquentLifetimeServiceProvider"
Usage
To use this package you need to add the EloquentLifetime
trait to your model.
use Paulund\EloquentLifetime\EloquentLifetime; class Post extends Model { use EloquentLifetime; }
This will then need you to add a lifetime method to your model which will return the carbon object to check the
created_at
date against.
public function lifetime(): Carbon { return now()->subDays(10); }
The above will then delete any model that has been created over 10 days ago.
Manually run command
By default the scheduled command is turned off and therefore you will need to manually run the artisan command to delete the models.
php artisan model:lifetime
Scheduled command
If you will like the command to run automatically you can add the following env to your application.
ELOQUENT_LIFETIME_SCHEDULED_COMMAND_ENABLED=true
By default this will run at midnight everyday but you can change this by adding the following to your env file.
ELOQUENT_LIFETIME_SCHEDULE=hourly, daily, weekly
Models folder
By default the package will look in the app/Models
folder for the models to delete. If you have your models in a different folder you can change this by adding the following to your env file.
ELOQUENT_LIFETIME_MODELS_FOLDER=app/somewhere/Models
Testing
vendor/bin/testbench workbench:install
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.