1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Activitylog\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Spatie\Activitylog\Contracts\Activity as ActivityContract; |
11
|
|
|
|
12
|
|
|
class Activity extends Model implements ActivityContract |
13
|
|
|
{ |
14
|
|
|
public $guarded = []; |
15
|
|
|
|
16
|
|
|
protected $casts = [ |
17
|
|
|
'properties' => 'collection', |
18
|
|
|
]; |
19
|
|
|
|
20
|
384 |
|
public function __construct(array $attributes = []) |
21
|
|
|
{ |
22
|
384 |
|
if (! isset($this->connection)) { |
23
|
380 |
|
$this->setConnection(config('activitylog.database_connection')); |
24
|
|
|
} |
25
|
|
|
|
26
|
384 |
|
if (! isset($this->table)) { |
27
|
380 |
|
$this->setTable(config('activitylog.table_name')); |
28
|
|
|
} |
29
|
|
|
|
30
|
384 |
|
parent::__construct($attributes); |
31
|
384 |
|
} |
32
|
|
|
|
33
|
232 |
|
public function subject(): MorphTo |
34
|
|
|
{ |
35
|
232 |
|
if (config('activitylog.subject_returns_soft_deleted_models')) { |
36
|
4 |
|
return $this->morphTo()->withTrashed(); |
37
|
|
|
} |
38
|
|
|
|
39
|
228 |
|
return $this->morphTo(); |
40
|
|
|
} |
41
|
|
|
|
42
|
48 |
|
public function causer(): MorphTo |
43
|
|
|
{ |
44
|
48 |
|
return $this->morphTo(); |
45
|
|
|
} |
46
|
|
|
|
47
|
16 |
|
public function getExtraProperty(string $propertyName) |
48
|
|
|
{ |
49
|
16 |
|
return Arr::get($this->properties->toArray(), $propertyName); |
50
|
|
|
} |
51
|
|
|
|
52
|
148 |
|
public function changes(): Collection |
53
|
|
|
{ |
54
|
148 |
|
if (! $this->properties instanceof Collection) { |
55
|
|
|
return new Collection(); |
56
|
|
|
} |
57
|
|
|
|
58
|
148 |
|
return $this->properties->only(['attributes', 'old']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getChangesAttribute(): Collection |
62
|
|
|
{ |
63
|
|
|
return $this->changes(); |
64
|
|
|
} |
65
|
|
|
|
66
|
16 |
|
public function scopeInLog(Builder $query, ...$logNames): Builder |
67
|
|
|
{ |
68
|
16 |
|
if (is_array($logNames[0])) { |
69
|
4 |
|
$logNames = $logNames[0]; |
70
|
|
|
} |
71
|
|
|
|
72
|
16 |
|
return $query->whereIn('log_name', $logNames); |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
public function scopeCausedBy(Builder $query, Model $causer): Builder |
76
|
|
|
{ |
77
|
|
|
return $query |
78
|
8 |
|
->where('causer_type', $causer->getMorphClass()) |
79
|
8 |
|
->where('causer_id', $causer->getKey()); |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
public function scopeForSubject(Builder $query, Model $subject): Builder |
83
|
|
|
{ |
84
|
|
|
return $query |
85
|
8 |
|
->where('subject_type', $subject->getMorphClass()) |
86
|
8 |
|
->where('subject_id', $subject->getKey()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|