1 | <?php |
||
17 | trait ModelEventLogger |
||
18 | { |
||
19 | /** |
||
20 | * Automatically boot with Model, and register Events handler. |
||
21 | */ |
||
22 | 14 | protected static function boot() |
|
23 | { |
||
24 | 14 | parent::boot(); |
|
25 | |||
26 | 14 | foreach (static::getRecordActivityEvents() as $eventName) { |
|
27 | static::$eventName(function (Model $model) use ($eventName) { |
||
28 | try { |
||
29 | 14 | $reflect = new \ReflectionClass($model); |
|
30 | |||
31 | 14 | return Activity::create([ |
|
32 | 14 | 'causer_id' => auth()->id(), |
|
|
|||
33 | 14 | 'causer_type' => auth()->check() ? get_class(auth()->user()) : null, |
|
34 | 14 | 'subject_id' => $model->id, |
|
35 | 14 | 'subject_type' => get_class($model), |
|
36 | 14 | 'action' => static::getActionName($eventName), |
|
37 | 14 | 'description' => ucfirst($eventName).' a '.$reflect->getShortName(), |
|
38 | 14 | 'old' => json_encode($model->getOriginal()), |
|
39 | 14 | 'new' => json_encode($model->getDirty()), |
|
40 | 14 | 'route' => (php_sapi_name() === 'cli' or defined('STDIN')) ? null : request()->url(), |
|
41 | ]); |
||
42 | } catch (\Exception $e) { |
||
43 | echo $e->getMessage(); |
||
44 | } |
||
45 | 14 | }); |
|
46 | } |
||
47 | 14 | } |
|
48 | |||
49 | /** |
||
50 | * Set the default events to be recorded if the $recordEvents |
||
51 | * property does not exist on the model. |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | 14 | protected static function getRecordActivityEvents() |
|
68 | |||
69 | /** |
||
70 | * Return Suitable action name for Supplied Event. |
||
71 | * |
||
72 | * @param $event |
||
73 | * @return string |
||
74 | */ |
||
75 | 14 | protected static function getActionName($event) |
|
91 | } |
||
92 |
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: