1 | <?php |
||
8 | trait DetectsChanges |
||
9 | { |
||
10 | protected $oldAttributes = []; |
||
11 | |||
12 | protected static function bootDetectsChanges() |
||
25 | |||
26 | public function attributesToBeLogged(): array |
||
34 | |||
35 | public function attributeValuesToBeLogged(string $processingEvent): array |
||
36 | { |
||
37 | if (! count($this->attributesToBeLogged())) { |
||
38 | return []; |
||
39 | } |
||
40 | |||
41 | $properties['attributes'] = static::logChanges($this->exists ? $this->fresh() : $this); |
||
42 | |||
43 | if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { |
||
44 | $nullProperties = array_fill_keys(array_keys($properties['attributes']), null); |
||
45 | |||
46 | $properties['old'] = array_merge($nullProperties, $this->oldAttributes); |
||
47 | } |
||
48 | |||
49 | return $properties; |
||
50 | } |
||
51 | |||
52 | public static function logChanges(Model $model): array |
||
64 | |||
65 | protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array |
||
77 | } |
||
78 |
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: