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