1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash\Eloquent; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @mixin \Illuminate\Database\Eloquent\Model |
19
|
|
|
* @property \Illuminate\Contracts\Auth\Authenticatable $creator |
20
|
|
|
* @property \Illuminate\Contracts\Auth\Authenticatable $editor |
21
|
|
|
* @property \Illuminate\Contracts\Auth\Authenticatable $destroyer |
22
|
|
|
*/ |
23
|
|
|
trait UserIdentities |
24
|
|
|
{ |
25
|
|
|
protected $userIdentities = true; |
26
|
|
|
protected $columnCreatedBy = 'created_by'; |
27
|
|
|
protected $columnUpdatedBy = 'updated_by'; |
28
|
|
|
protected $columnDeletedBy = 'deleted_by'; |
29
|
|
|
|
30
|
|
|
protected static function bootUserIdentities() |
31
|
|
|
{ |
32
|
|
|
// Creating |
33
|
|
|
static::creating(function (Model $model) { |
34
|
|
|
if (! $model->usesUserIdentities()) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (is_null($model->{$model->columnCreatedBy})) { |
39
|
|
|
$model->{$model->columnCreatedBy} = auth()->id(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (is_null($model->{$model->columnUpdatedBy})) { |
43
|
|
|
$model->{$model->columnUpdatedBy} = auth()->id(); |
44
|
|
|
} |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
// Updating |
48
|
|
|
static::updating(function (Model $model) { |
49
|
|
|
if (! $model->usesUserIdentities()) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$model->{$model->columnUpdatedBy} = auth()->id(); |
|
|
|
|
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
// Delete/Restore |
57
|
|
|
if (static::usingSoftDeletes()) { |
58
|
|
|
|
59
|
|
|
static::deleting(function (Model $model) { |
60
|
|
|
if (! $model->usesUserIdentities()) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (is_null($model->{$model->columnDeletedBy})) { |
65
|
|
|
$model->{$model->columnDeletedBy} = auth()->id(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$model->save(); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
static::restoring(function (Model $model) { |
72
|
|
|
if (! $model->usesUserIdentities()) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$model->{$model->columnDeletedBy} = null; |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function usingSoftDeletes(): bool |
83
|
|
|
{ |
84
|
|
|
static $usingSoftDeletes; |
85
|
|
|
|
86
|
|
|
if (is_null($usingSoftDeletes)) { |
87
|
|
|
return $usingSoftDeletes = in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive(get_called_class())); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $usingSoftDeletes; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function creator(): BelongsTo |
94
|
|
|
{ |
95
|
|
|
return $this->belongsTo($this->getUserClass(), $this->columnCreatedBy); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function editor(): BelongsTo |
99
|
|
|
{ |
100
|
|
|
return $this->belongsTo($this->getUserClass(), $this->columnUpdatedBy); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function destroyer(): BelongsTo |
104
|
|
|
{ |
105
|
|
|
return $this->belongsTo($this->getUserClass(), $this->columnDeletedBy); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function usesUserIdentities(): bool |
109
|
|
|
{ |
110
|
|
|
return $this->userIdentities; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function stopUserIdentities(): self |
114
|
|
|
{ |
115
|
|
|
$this->userIdentities = false; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function startUserIdentities(): self |
121
|
|
|
{ |
122
|
|
|
$this->userIdentities = true; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function getUserClass(): string |
128
|
|
|
{ |
129
|
|
|
if (get_class(auth()) === \Illuminate\Auth\Guard::class) { |
130
|
|
|
return auth()->getProvider()->getModel(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return auth()->guard()->getProvider()->getModel(); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: