PHPackages                             roberthucks/selfdestruct - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. roberthucks/selfdestruct

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

roberthucks/selfdestruct
========================

A package used to give Laravel Models the ability to self-delete after a specified time.

v1.3(7y ago)0543[1 issues](https://github.com/Robert-Hucks/SelfDestruct/issues)MITPHP

Since Jan 7Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Robert-Hucks/SelfDestruct)[ Packagist](https://packagist.org/packages/roberthucks/selfdestruct)[ Docs](https://github.com/Robert-Hucks/SelfDestruct)[ RSS](/packages/roberthucks-selfdestruct/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (4)DependenciesVersions (5)Used By (0)

Self Destruct
=============

[](#self-destruct)

This package was built with the goal in mind of creating a repeatable way of having models delete themselves after a specified amount of time. Currently, the package is fairly basic but could be expanded upon to allow deeper customisation and the ability of having per-record expiration times rather than scoped to each model.

What's in the package
---------------------

[](#whats-in-the-package)

- [SelfDestruct Trait](https://github.com/Robert-Hucks/SelfDestruct/blob/master/src/Traits/SelfDestruct.php)

This can be used on `Model`s to give it the ability to delete after a specified time.

- [Migration](https://github.com/Robert-Hucks/SelfDestruct/blob/master/src/Database/migrations/2018_12_21_095154_create_self_destruct_table.php)

Table that is used to manage the destruction of `Model`s.

How it works
------------

[](#how-it-works)

This package works by using the provided `Trait` on a `Model` which hooks in to the `created` event. When a `Model` is created, it takes the sum of `created_at` and `life_time` and stores it in a seperate table. Then, every minute this table is checked for expired records and each `Model` is deleted.

How to use
----------

[](#how-to-use)

1. Adding the package to your project.

```
composer require roberthucks/selfdestruct
```

2. Run migration to create the table

```
php artisan migrate
```

3. Configure models

The first step here is to add the `Trait` to the `Model`. Add `use RobertHucks\SelfDestruct\Traits\SelfDestruct;` and then user it inside your `Model`s class like so `use SelfDestruct;`Once these have been added to your `Model` you need to then add a property called `$life_time`. This is the amount of seconds that a `Model` should stay alive for.

Here is how a `Model` looks when modified:

```
