PHP code example of maize-tech / laravel-prunable-fields

1. Go to this page and download the library: Download maize-tech/laravel-prunable-fields library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maize-tech / laravel-prunable-fields example snippets


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,
    ],

];
bash
php artisan vendor:publish --tag="prunable-fields-config"
 php


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Maize\PrunableFields\PrunableFields;

class User extends Model
{
    use PrunableFields;

    protected $fillable = [
        'first_name',
        'last_name',
        'email',
    ];

    protected $prunable = [
        'first_name' => null,
        'last_name' => null,
    ];
    
    public function prunableFields(): Builder
    {
        return static::query()
            ->whereDate('created_at', '<=', now()->subDay());
    }
    
    protected function pruningFields(): void
    {
        logger()->warning("User {$this->getKey()} is being pruned");
    }

    protected function prunedFields(): void
    {
        logger()->warning("User {$this->getKey()} has been pruned");
    }
}
 php
$schedule->command('model:prune-fields')->daily();
 php
$schedule->command('model:prune-fields', [
    '--except' => [PleaseLetMeCleanThisModelByHandsThankYou::class],
])->daily();