Total Complexity | 14 |
Total Lines | 57 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
8 | trait ChangeByUser |
||
9 | { |
||
10 | /** |
||
11 | * automatically boot laravel model traits... |
||
12 | */ |
||
13 | public static function bootChangeByUser() |
||
14 | { |
||
15 | static::creating(function ($model) { |
||
16 | if (App::runningInConsole() || ! Auth::check()) { |
||
17 | return true; |
||
18 | } |
||
19 | if ($model->getCreatedByColumn()) { |
||
20 | $model->{$model->getUpdatedByColumn()} = Auth::user()->getKey(); |
||
21 | } |
||
22 | if ($model->getUpdatedByColumn()) { |
||
23 | $model->{$model->getUpdatedByColumn()} = Auth::user()->getKey(); |
||
24 | } |
||
25 | |||
26 | return true; |
||
27 | }); |
||
28 | |||
29 | static::updating(function ($model) { |
||
30 | if (App::runningInConsole() || ! Auth::check()) { |
||
31 | return true; |
||
32 | } |
||
33 | if ($model->getUpdatedByColumn()) { |
||
34 | $model->{$model->getUpdatedByColumn()} = Auth::user()->getKey(); |
||
35 | } |
||
36 | |||
37 | return true; |
||
38 | }); |
||
39 | |||
40 | static::deleting(function ($model) { |
||
41 | if (App::runningInConsole() || ! Auth::check()) { |
||
42 | return true; |
||
43 | } |
||
44 | if ($model->getDeletedByColumn()) { |
||
45 | $model->{$model->getDeletedByColumn()} = Auth::user()->getKey(); |
||
46 | } |
||
47 | |||
48 | return true; |
||
49 | }); |
||
50 | } |
||
51 | |||
52 | public function getCreatedByColumn(): string |
||
53 | { |
||
54 | return 'created_by'; |
||
55 | } |
||
56 | |||
57 | public function getUpdatedByColumn(): string |
||
60 | } |
||
61 | |||
62 | public function getDeletedByColumn(): string |
||
65 | } |
||
66 | } |
||
67 |