1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Activitylog\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
9
|
|
|
use Spatie\Activitylog\Contracts\Activity as ActivityContract; |
10
|
|
|
|
11
|
|
|
class Activity extends Model implements ActivityContract |
12
|
|
|
{ |
13
|
|
|
public $guarded = []; |
14
|
|
|
|
15
|
|
|
protected $casts = [ |
16
|
|
|
'properties' => 'collection', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
public function __construct(array $attributes = []) |
20
|
|
|
{ |
21
|
|
|
if (! isset($this->table)) { |
22
|
|
|
$this->setTable(config('activitylog.table_name')); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
parent::__construct($attributes); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function subject(): MorphTo |
29
|
|
|
{ |
30
|
|
|
if (config('activitylog.subject_returns_soft_deleted_models')) { |
31
|
|
|
return $this->morphTo()->withTrashed(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->morphTo(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function causer(): MorphTo |
38
|
|
|
{ |
39
|
|
|
return $this->morphTo(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getExtraProperty(string $propertyName) |
43
|
|
|
{ |
44
|
|
|
return array_get($this->properties->toArray(), $propertyName); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function changes(): Collection |
48
|
|
|
{ |
49
|
|
|
if (! $this->properties instanceof Collection) { |
|
|
|
|
50
|
|
|
return new Collection(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->properties->only(['attributes', 'old']); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getChangesAttribute(): Collection |
57
|
|
|
{ |
58
|
|
|
return $this->changes(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function scopeInLog(Builder $query, ...$logNames): Builder |
62
|
|
|
{ |
63
|
|
|
if (is_array($logNames[0])) { |
64
|
|
|
$logNames = $logNames[0]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $query->whereIn('log_name', $logNames); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function scopeCausedBy(Builder $query, Model $causer): Builder |
71
|
|
|
{ |
72
|
|
|
return $query |
73
|
|
|
->where('causer_type', $causer->getMorphClass()) |
74
|
|
|
->where('causer_id', $causer->getKey()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function scopeForSubject(Builder $query, Model $subject): Builder |
78
|
|
|
{ |
79
|
|
|
return $query |
80
|
|
|
->where('subject_type', $subject->getMorphClass()) |
81
|
|
|
->where('subject_id', $subject->getKey()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.