1 | <?php |
||
8 | trait DetectsChanges |
||
9 | { |
||
10 | protected $oldAttributes = []; |
||
11 | |||
12 | protected static function bootDetectsChanges() |
||
13 | { |
||
14 | if (static::eventsToBeRecorded()->contains('updated')) { |
||
15 | static::updating(function (Model $model) { |
||
16 | //temporary hold the original attributes on the model |
||
17 | //as we'll need these in the updating event |
||
18 | $oldValues = $model->replicate()->setRawAttributes($model->getOriginal()); |
||
19 | |||
20 | $model->oldAttributes = static::logChanges($oldValues); |
||
21 | }); |
||
22 | } |
||
23 | } |
||
24 | |||
25 | public function attributesToBeLogged(): array |
||
33 | |||
34 | public function shouldLogDirtyOnly(): bool |
||
35 | { |
||
36 | if (! isset(static::$logDirtyOnly)) { |
||
37 | return false; |
||
38 | } |
||
39 | |||
40 | return static::$logDirtyOnly; |
||
41 | } |
||
42 | |||
43 | public function attributeValuesToBeLogged(string $processingEvent): array |
||
65 | |||
66 | public static function logChanges(Model $model): array |
||
78 | |||
79 | protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array |
||
91 | } |
||
92 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: