paulund/eloquent-lifetime

There is no license information available for the latest version (0.0.1) of this package.

Laravel package to delete records after a certain amount of time

0.0.1 2024-11-24 11:36 UTC

This package is auto-updated.

Last update: 2024-11-24 12:18:49 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Eloquent Lifetime

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.