1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
9
|
|
|
|
10
|
|
|
trait Auditable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Register a creating model event with the dispatcher. |
14
|
|
|
* |
15
|
|
|
* @param \Closure|string $callback |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
abstract public static function creating($callback); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Register an updating model event with the dispatcher. |
23
|
|
|
* |
24
|
|
|
* @param \Closure|string $callback |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
abstract public static function updating($callback); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Define an inverse one-to-one or many relationship. |
32
|
|
|
* |
33
|
|
|
* @param string $related |
34
|
|
|
* @param string $foreignKey |
|
|
|
|
35
|
|
|
* @param string $ownerKey |
|
|
|
|
36
|
|
|
* @param string $relation |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
39
|
|
|
*/ |
40
|
|
|
abstract public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Boot the Auditable trait for the model. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public static function bootAuditable() |
48
|
|
|
{ |
49
|
|
|
static::creating(function (Model $model) { |
50
|
|
|
$model->created_by || $model->created_by = auth()->id(); |
|
|
|
|
51
|
|
|
$model->updated_by || $model->updated_by = auth()->id(); |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
static::updating(function (Model $model) { |
55
|
|
|
$model->isDirty('updated_by') || $model->updated_by = auth()->id(); |
|
|
|
|
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get user model who created the record. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
63
|
|
|
*/ |
64
|
|
|
public function creator(): BelongsTo |
65
|
|
|
{ |
66
|
|
|
$userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $this->belongsTo($userModel, 'created_by'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get user model who updated the record. |
73
|
|
|
* |
74
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
75
|
|
|
*/ |
76
|
|
|
public function updater(): BelongsTo |
77
|
|
|
{ |
78
|
|
|
$userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'); |
|
|
|
|
79
|
|
|
|
80
|
|
|
return $this->belongsTo($userModel, 'updated_by'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.