1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neurony\Revisions\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Neurony\Revisions\Contracts\RevisionModelContract; |
9
|
|
|
|
10
|
|
|
class Revision extends Model implements RevisionModelContract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The database table. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $table = 'revisions'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The attributes that are mass assignable. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $fillable = [ |
25
|
|
|
'user_id', |
26
|
|
|
'revisionable_id', |
27
|
|
|
'revisionable_type', |
28
|
|
|
'metadata', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The attributes that are casted to a specific type. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $casts = [ |
37
|
|
|
'metadata' => 'array', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Revision belongs to user. |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
44
|
|
|
*/ |
45
|
|
|
public function user() |
46
|
|
|
{ |
47
|
|
|
$user = config('revisions.user_model', null); |
48
|
|
|
|
49
|
|
|
if ($user && class_exists($user)) { |
50
|
|
|
return $this->belongsTo($user, 'user_id'); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all of the owning revisionable models. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
58
|
|
|
*/ |
59
|
|
|
public function revisionable() |
60
|
|
|
{ |
61
|
|
|
return $this->morphTo(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Filter the query by the given user id. |
66
|
|
|
* |
67
|
|
|
* @param Builder $query |
68
|
|
|
* @param Authenticatable|int $user |
69
|
|
|
*/ |
70
|
|
|
public function scopeWhereUser($query, Authenticatable $user): void |
71
|
|
|
{ |
72
|
|
|
$query->where('user_id', $user->id); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Filter the query by the given revisionable params (id, type). |
77
|
|
|
* |
78
|
|
|
* @param Builder $query |
79
|
|
|
* @param int $id |
80
|
|
|
* @param string $type |
81
|
|
|
*/ |
82
|
|
|
public function scopeWhereRevisionable($query, int $id, string $type) |
83
|
|
|
{ |
84
|
|
|
$query->where([ |
85
|
|
|
'revisionable_id' => $id, |
86
|
|
|
'revisionable_type' => $type, |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: