@@ -16,12 +16,16 @@ class AuditLog extends Model
1616 'user_id ' ,
1717 'tenant_id ' ,
1818 'event ' ,
19+ 'action ' ,
1920 'auditable_type ' ,
2021 'auditable_id ' ,
22+ 'auditable_label ' ,
2123 'old_values ' ,
2224 'new_values ' ,
2325 'ip_address ' ,
2426 'user_agent ' ,
27+ 'url ' ,
28+ 'module ' ,
2529 'created_at ' ,
2630 ];
2731
@@ -31,6 +35,8 @@ class AuditLog extends Model
3135 'created_at ' => 'datetime ' ,
3236 ];
3337
38+ protected $ dates = ['created_at ' ];
39+
3440 public function auditable (): MorphTo
3541 {
3642 return $ this ->morphTo ();
@@ -49,31 +55,73 @@ public function tenant(): BelongsTo
4955 /**
5056 * Record an audit log entry.
5157 *
52- * @param string $event
53- * @param Model|null $auditable
54- * @param array $oldValues
55- * @param array $newValues
56- * @param int|null $tenantId
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
5767 * @return static
5868 */
5969 public static function record (
60- string $ event ,
61- ? Model $ auditable = null ,
70+ string $ action ,
71+ $ model = null ,
6272 array $ oldValues = [],
6373 array $ newValues = [],
64- ? int $ tenantId = null
74+ $ moduleOrTenantId = null
6575 ): static {
76+ $ tenantId = null ;
77+ $ module = '' ;
78+
79+ if (is_int ($ moduleOrTenantId )) {
80+ $ tenantId = $ moduleOrTenantId ;
81+ } elseif (is_string ($ moduleOrTenantId )) {
82+ $ module = $ moduleOrTenantId ;
83+ }
84+
85+ if ($ tenantId === null ) {
86+ $ tenantId = auth ()->user ()?->tenant_id ?? 0 ;
87+ }
88+
6689 return static ::create ([
67- 'user_id ' => auth ()->id (),
68- 'tenant_id ' => $ tenantId ,
69- 'event ' => $ event ,
70- 'auditable_type ' => $ auditable ? get_class ($ auditable ) : null ,
71- 'auditable_id ' => $ auditable ?->getKey(),
72- 'old_values ' => $ oldValues ?: null ,
73- 'new_values ' => $ newValues ?: null ,
74- 'ip_address ' => request ()->ip (),
75- 'user_agent ' => request ()->userAgent (),
76- 'created_at ' => now (),
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 (),
77104 ]);
78105 }
106+
107+ /**
108+ * Get a human-readable summary of changes.
109+ */
110+ public function getChangeSummaryAttribute (): string
111+ {
112+ if ($ this ->old_values && $ this ->new_values ) {
113+ $ changedKeys = array_keys (array_diff_assoc (
114+ (array ) $ this ->new_values ,
115+ (array ) $ this ->old_values
116+ ));
117+
118+ if (empty ($ changedKeys )) {
119+ $ changedKeys = array_keys ((array ) $ this ->new_values );
120+ }
121+
122+ return implode (', ' , $ changedKeys ) . ' changed ' ;
123+ }
124+
125+ return $ this ->action ?? $ this ->event ?? '' ;
126+ }
79127}
0 commit comments