You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Here you may specify the list of fully qualified class names of prunable
36
+
| models.
37
+
| All models listed here will be pruned when executing the model:prune-fields
38
+
| command without passing the --model option.
39
+
|
40
+
*/
41
+
42
+
'models' => [
43
+
// \App\Models\User::class,
44
+
],
45
+
46
+
];
47
+
```
48
+
49
+
## Usage
50
+
51
+
### Prunable models
52
+
53
+
To use the package, simply add the `Maize\PrunableFields\PrunableFields` trait to all models you want to clean.
54
+
55
+
Once done, you can define the list of attributes who should be cleaned up by implementing the `$prunable` class property.
56
+
The array key should be the attribute name, whereas the array value should be the value you want the attribute to be updated to.
57
+
58
+
After that, implement the `prunableFields` method which should return an Eloquent query builder that resolves the models that should be cleaned up.
59
+
60
+
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.
61
+
62
+
Here's an example model including the `PrunableFields` trait:
63
+
64
+
```php
65
+
<?php
66
+
67
+
namespace App\Models;
68
+
69
+
use Illuminate\Database\Eloquent\Model;
70
+
use Maize\PrunableFields\PrunableFields;
71
+
72
+
class User extends Model
73
+
{
74
+
use PrunableFields;
75
+
76
+
protected $fillable = [
77
+
'first_name',
78
+
'last_name',
79
+
'email',
80
+
];
81
+
82
+
protected $prunable = [
83
+
'first_name' => null,
84
+
'last_name' => null,
85
+
];
86
+
87
+
public function prunableFields(): Builder
88
+
{
89
+
return static::query()
90
+
->whereDate('created_at', '<=', now()->subDay());
91
+
}
92
+
93
+
protected function pruningFields(): void
94
+
{
95
+
logger()->warning("User {$this->getKey()} is being pruned");
96
+
}
97
+
98
+
protected function prunedFields()
99
+
{
100
+
logger()->warning("User {$this->getKey()} has been pruned");
101
+
}
102
+
}
103
+
```
104
+
105
+
All you have to do now is including the model's class name in `models` attribute under `config/prunable-fields.php`:
106
+
107
+
```php
108
+
'models' => [
109
+
\App\Models\User::class,
110
+
],
111
+
```
112
+
113
+
That's it! From now on, the `model:prune-fields` command will do all the magic.
114
+
115
+
In our example, all users created before the current day will be updated with a null value for both `first_name` and `last_name` attributes.
116
+
117
+
### Mass prunable models
118
+
119
+
When using the `MassPrunableFields` trait all models will be updated with a raw database query.
120
+
121
+
In this case, `pruningFields` and `prunedFields` methods will not be invoked, and models will not fire the `updating` or `updated` events.
122
+
123
+
This way there is no need to retrieve all models before updating them, making the command execution way faster when working with a large number of entries.
124
+
125
+
```php
126
+
<?php
127
+
128
+
namespace App\Models;
129
+
130
+
use Illuminate\Database\Eloquent\Model;
131
+
use Maize\PrunableFields\MassPrunableFields;
132
+
133
+
class User extends Model
134
+
{
135
+
use MassPrunableFields;
136
+
137
+
protected $fillable = [
138
+
'first_name',
139
+
'last_name',
140
+
'email',
141
+
];
142
+
143
+
protected $prunable = [
144
+
'first_name' => null,
145
+
'last_name' => null,
146
+
];
147
+
148
+
public function prunableFields(): Builder
149
+
{
150
+
return static::query()
151
+
->whereDate('created_at', '<=', now()->subDay());
152
+
}
153
+
}
154
+
```
155
+
156
+
### Scheduling models cleanup
157
+
158
+
The package is pretty useful when you automatize the execution of the `model:prune-fields` command, using Laravel's scheduling.
159
+
All you have to do is add the following instruction to the `schedule` method of the console kernel (usually located under the `App\Console` directory):
By default, when executing the `model:prune-fields` command the package will take all prunable models specified in `models` attribute under `config/prunable-fields.php`.
166
+
167
+
If you want to restrict the model list you want to automatically clean up, you can pass the `--model` option to the command:
168
+
169
+
```php
170
+
$schedule->command('model:prune-fields', [
171
+
'--model' => [User::class],
172
+
])->daily();
173
+
```
174
+
175
+
Alternatively, you can clean up all models listed in your config and exclude some of them with the `--execpt` command option:
0 commit comments