44
55use Illuminate \Database \Eloquent \Model ;
66use Illuminate \Database \Eloquent \Relations \BelongsTo ;
7- use Illuminate \Database \Eloquent \Relations \MorphTo ;
87
98class AuditLog extends Model
109{
11- public const UPDATED_AT = null ;
12-
13- public $ timestamps = false ;
14-
1510 protected $ fillable = [
16- 'user_id ' ,
1711 'tenant_id ' ,
18- 'event ' ,
12+ 'user_id ' ,
1913 'action ' ,
2014 'auditable_type ' ,
2115 'auditable_id ' ,
22- 'auditable_label ' ,
2316 'old_values ' ,
2417 'new_values ' ,
2518 'ip_address ' ,
2619 'user_agent ' ,
27- 'url ' ,
28- 'module ' ,
29- 'created_at ' ,
3020 ];
3121
3222 protected $ casts = [
33- 'old_values ' => 'array ' ,
34- 'new_values ' => 'array ' ,
35- 'created_at ' => 'datetime ' ,
23+ 'old_values ' => 'array ' ,
24+ 'new_values ' => 'array ' ,
3625 ];
3726
38- protected $ dates = ['created_at ' ];
39-
40- public function auditable (): MorphTo
41- {
42- return $ this ->morphTo ();
43- }
44-
4527 public function user (): BelongsTo
4628 {
4729 return $ this ->belongsTo (\App \Models \User::class);
@@ -55,15 +37,11 @@ public function tenant(): BelongsTo
5537 /**
5638 * Record an audit log entry.
5739 *
58- * Supports two call styles:
59- * record(string $action, $model, array $old, array $new, string $module) -- new style
60- * record(string $event, $model, array $old, array $new, int $tenantId) -- legacy style
61- *
62- * @param string $action
63- * @param Model|null $model
64- * @param array $oldValues
65- * @param array $newValues
66- * @param string|int|null $moduleOrTenantId
40+ * @param string $action
41+ * @param Model|null $model
42+ * @param array $oldValues
43+ * @param array $newValues
44+ * @param mixed $moduleOrTenantId ignored (kept for backward compat)
6745 * @return static
6846 */
6947 public static function record (
@@ -74,33 +52,25 @@ public static function record(
7452 $ moduleOrTenantId = null
7553 ): static {
7654 $ tenantId = null ;
77- $ module = '' ;
7855
7956 if (is_int ($ moduleOrTenantId )) {
8057 $ tenantId = $ moduleOrTenantId ;
81- } elseif (is_string ($ moduleOrTenantId )) {
82- $ module = $ moduleOrTenantId ;
8358 }
8459
8560 if ($ tenantId === null ) {
86- $ tenantId = auth ()->user ()?->tenant_id ?? 0 ;
61+ $ tenantId = $ model -> tenant_id ?? auth ()->user ()?->tenant_id ?? 0 ;
8762 }
8863
8964 return static ::create ([
90- 'tenant_id ' => $ tenantId ,
91- 'user_id ' => auth ()->id (),
92- 'event ' => $ action ,
93- 'action ' => $ action ,
94- 'auditable_type ' => $ model ? get_class ($ model ) : null ,
95- 'auditable_id ' => $ model ?->getKey(),
96- 'auditable_label ' => $ model ?->name ?? $ model ?->title ?? $ model ?->subject ?? null ,
97- 'old_values ' => $ oldValues ?: null ,
98- 'new_values ' => $ newValues ?: null ,
99- 'ip_address ' => request ()?->ip(),
100- 'user_agent ' => request ()?->userAgent(),
101- 'url ' => request ()?->fullUrl(),
102- 'module ' => $ module ,
103- 'created_at ' => now (),
65+ 'tenant_id ' => $ tenantId ,
66+ 'user_id ' => auth ()->id (),
67+ 'action ' => $ action ,
68+ 'auditable_type ' => $ model ? get_class ($ model ) : null ,
69+ 'auditable_id ' => $ model ?->getKey(),
70+ 'old_values ' => $ oldValues ?: null ,
71+ 'new_values ' => $ newValues ?: null ,
72+ 'ip_address ' => request ()?->ip(),
73+ 'user_agent ' => request ()?->userAgent(),
10474 ]);
10575 }
10676
@@ -122,6 +92,6 @@ public function getChangeSummaryAttribute(): string
12292 return implode (', ' , $ changedKeys ) . ' changed ' ;
12393 }
12494
125- return $ this ->action ?? $ this -> event ?? '' ;
95+ return $ this ->action ?? '' ;
12696 }
12797}
0 commit comments