PHPackages                             biiiiiigmonster/laravel-clearable - 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. [Database &amp; ORM](/categories/database)
4. /
5. biiiiiigmonster/laravel-clearable

ActiveLibrary[Database &amp; ORM](/categories/database)

biiiiiigmonster/laravel-clearable
=================================

Laravel framework eloquent trait

v0.4.0(3y ago)25[3 PRs](https://github.com/biiiiiigmonster/laravel-clearable/pulls)MITPHP

Since Oct 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/biiiiiigmonster/laravel-clearable)[ Packagist](https://packagist.org/packages/biiiiiigmonster/laravel-clearable)[ RSS](/packages/biiiiiigmonster-laravel-clearable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (6)Versions (9)Used By (0)

English | [中文](./README-CN.md)

LARAVEL ELOQUENT TRAIT
======================

[](#laravel-eloquent-trait)

 [![](https://camo.githubusercontent.com/ffe7adf17b1d38c79e0e81f80884e0e21f43129b2b96c7a123ea3bd032f09cb2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d3733383944382e7376673f7374796c653d666c6174)](https://github.com/biiiiiigmonster/laravel-clearable/blob/master/LICENSE) [![](https://camo.githubusercontent.com/c0a170606fb4be2e3e51810f6c67e96a2925c1fc6c2d6a7014f960e1a731a32b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62696969696969676d6f6e737465722f6c61726176656c2d636c65617261626c652e7376673f636f6c6f723d343039394445)](https://github.com/biiiiiigmonster/laravel-clearable/releases) [![](https://camo.githubusercontent.com/8745fbaa7b08244ebc1a8c69d0daf467d64ed789fc4e59a41d7a22545f8ada9b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62696969696969676d6f6e737465722f6c61726176656c2d636c65617261626c652e7376673f636f6c6f723d)](https://packagist.org/packages/biiiiiigmonster/laravel-clearable)

Environment
===========

[](#environment)

- laravel &gt;= 9

Installation
============

[](#installation)

```
composer require biiiiiigmonster/laravel-clearable
```

Introductions
=============

[](#introductions)

`relation` is powerful, it can help us manage complex relationship's data. Usually, as the last section of the data life cycle, the "delete" behavior receives less attention. We often neglect the processing of the associated model data while deleting the data itself, the business will also be damaged due to these residual data.

This package can easily help you manage these related data's deletion relationships, with simple definitions. Let's try it!

Usage
-----

[](#usage)

For example, `User` model related `Post` model, it's also hoped that the associated `Post` model can be deleted after the `User` model deleted:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the posts for the user.
     *
     * @return HasMany
     */
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

```

To accomplish this, you may add the `BiiiiiigMonster\Clears\Concerns\HasClears` trait to the models you would like to auto-clear. After adding one of the traits to the model, add the attribute name to the `clears` property of your model.

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use BiiiiiigMonster\Clears\Concerns\HasClears;

class User extends Model
{
    use HasClears;

    /**
     * The relationships that will be auto-clear when deleted.
     *
     * @var array
     */
    protected $clears = ['posts'];
}

```

Once the relationship has been added to the `clears` list, it will be auto-clear when deleted.

Clear Configuration
-------------------

[](#clear-configuration)

### Custom Clear

[](#custom-clear)

Sometimes you may occasionally need to define your own clear's logic, You may accomplish this by defining a class that implements the `InvokableClear` interface.

To generate a new clear object, you may use the `make:clear` Artisan command. we will place the new rule in the `app/Clears` directory. If this directory does not exist, We will create it when you execute the Artisan command to create your clear:

```
php artisan make:clear PostWithoutReleasedClear
```

Once the clear has been created, we are ready to define its behavior. A clear object contains a single method: `__invoke`. This method will determine whether the relation data is cleared.

```
