PHPackages                             maize-tech/laravel-prunable-fields - 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. maize-tech/laravel-prunable-fields

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

maize-tech/laravel-prunable-fields
==================================

Laravel Prunable Fields

1.3.0(1y ago)421.6k8[4 PRs](https://github.com/maize-tech/laravel-prunable-fields/pulls)MITPHPPHP ^8.0CI passing

Since Jun 10Pushed 2mo ago4 watchersCompare

[ Source](https://github.com/maize-tech/laravel-prunable-fields)[ Packagist](https://packagist.org/packages/maize-tech/laravel-prunable-fields)[ Docs](https://github.com/maize-tech/laravel-prunable-fields)[ GitHub Sponsors](https://github.com/maize-tech)[ RSS](/packages/maize-tech-laravel-prunable-fields/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (4)Dependencies (12)Versions (10)Used By (0)

Laravel Prunable Fields
=======================

[](#laravel-prunable-fields)

   ![Social Card of Laravel Prunable Fields](/art/socialcard-light.png)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5c66db943c92ec233ba432ef5d3f870c37ffad9c5054e1f4973bc1c95aab3405/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61697a652d746563682f6c61726176656c2d7072756e61626c652d6669656c64732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-prunable-fields)[![GitHub Tests Action Status](https://camo.githubusercontent.com/7b4d17f70b19832f9e562dd4cc2886828d2d68532e4f9d9b99804543f0129170/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d7072756e61626c652d6669656c64732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-prunable-fields/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/cae1501e333a8a00303564df04264a6dff220c47e15d961ebc8352d0a50018e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d7072756e61626c652d6669656c64732f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-prunable-fields/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/f09c5182dbac8582d62319d790172c003ab80c00e2c9db1bc477b283765a2f4c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61697a652d746563682f6c61726176656c2d7072756e61626c652d6669656c64732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-prunable-fields)

This package allows you to clean model fields with an easy command. The feature is highly inspired by Laravel's Prunable core feature, and allows you to easily adapt all your existing models.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require maize-tech/laravel-prunable-fields
```

You can publish the config file with:

```
php artisan vendor:publish --tag="prunable-fields-config"
```

This is the contents of the published config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Prunable models
    |--------------------------------------------------------------------------
    |
    | Here you may specify the list of fully qualified class names of prunable
    | models.
    | All models listed here will be pruned when executing the model:prune-fields
    | command without passing the --model option.
    |
    */

    'models' => [
        // \App\Models\User::class,
    ],

];
```

Usage
-----

[](#usage)

### Prunable models

[](#prunable-models)

To use the package, simply add the `Maize\PrunableFields\PrunableFields` trait to all models you want to clean.

Once done, you can define the list of attributes who should be cleaned up by implementing the `$prunable` class property. The array key should be the attribute name, whereas the array value should be the value you want the attribute to be updated to.

After that, implement the `prunableFields` method which should return an Eloquent query builder that resolves the models that should be cleaned up.

If needed, you can also override the `pruningFields` and `prunedFields` methods (which are empty by default) to execute some actions before and after the model is being updated.

Here's an example model including the `PrunableFields` trait:

```
