@@ -8,53 +8,45 @@ Logger is a lightweight, opinionated activity logging package for Laravel that a
88You can install the package via composer:
99
1010``` bash
11- composer require gott/logger
11+ composer require gottvergessen/activity
1212```
1313
1414You can publish and run the migrations with:
1515
1616``` bash
17- php artisan logger :install
17+ php artisan activity :install
1818```
1919
20-
21- ### config/logger.php
20+ ### config/activity.php
2221
2322``` php
24- return [
25- 'enabled' => env('LOGGER_ENABLED', true),
26-
27- 'table' => 'activity_logs',
28-
29- 'default_log' => env('LOGGER_DEFAULT_LOG', 'default'),
30-
31- 'origin' => env('LOGGER_ORIGIN', 'web'),
32-
33- 'events' => [
34- 'created',
35- 'updated',
36- 'deleted',
37- 'restored',
38- ],
39-
40- 'ignore_attributes' => [
41- 'created_at',
42- 'updated_at',
43- 'deleted_at',
44- 'remember_token',
45- ],
23+ return [
24+ 'enabled' => true,
4625
47- 'use_diffs ' => true ,
26+ 'table ' => 'activity_logs' ,
4827
49- 'driver' => 'database',
50- ];
28+ 'default_log' => 'default',
5129
30+ 'events' => [
31+ 'created',
32+ 'updated',
33+ 'deleted',
34+ 'restored',
35+ ],
36+
37+ 'ignore_attributes' => [
38+ 'created_at',
39+ 'updated_at',
40+ 'deleted_at',
41+ 'remember_token',
42+ ],
43+ ];
5244```
5345
5446## Basic usage
5547
5648``` php
57- use Gottvergessen\Logger \Traits\TracksModelActivity;
49+ use Gottvergessen\Activity \Traits\TracksModelActivity;
5850
5951class User extends Authenticatable
6052{
@@ -64,8 +56,9 @@ class User extends Authenticatable
6456```
6557
6658## Ignore Specific Fields per Model
59+
6760``` php
68- use Gottvergessen\Logger \Traits\TracksModelActivity;
61+ use Gottvergessen\Activity \Traits\TracksModelActivity;
6962
7063class User extends Authenticatable
7164{
@@ -77,14 +70,61 @@ class User extends Authenticatable
7770 ];
7871
7972 /*
80- * Changes to the listed attributes will not appear in the activity properties
81- * If only ignored attributes change, no activity log entry is created
73+ * Changes to the listed attributes will not appear in the activity properties
74+ * If only ignored attributes change, no activity log entry is created
8275 */
8376}
8477```
78+
79+ ## Database Schema
80+
81+ The ` activity_logs ` table tracks the following information:
82+
83+ | Column | Type | Description |
84+ | --------| ------| -------------|
85+ | id | ID | Primary key |
86+ | event | string | The event type (created, updated, deleted, restored) |
87+ | action | string | Custom semantic action defined by the model (optional) |
88+ | log | string | Log category for grouping activities |
89+ | description | string | Description of the activity (optional) |
90+ | subject_type | string | The model class that was changed |
91+ | subject_id | int | The ID of the model that was changed |
92+ | causer_type | string | The model class of the user who made the change (optional) |
93+ | causer_id | int | The ID of the user who made the change (optional) |
94+ | properties | json | The model attributes that changed |
95+ | meta | json | Metadata including IP, user agent, HTTP method, and host |
96+ | batch_id | string | Batch ID for grouping related changes (optional) |
97+ | created_at | timestamp | When the activity was recorded |
98+ | updated_at | timestamp | When the activity record was last updated |
99+
100+ ## Eloquent Friendly
101+
102+ ``` php
103+ use Gottvergessen\Activity\Traits\TracksModelActivity;
104+ use Gottvergessen\Activity\Traits\InteractsWithActivity;
105+
106+ class User extends Authenticatable
107+ {
108+ use TracksModelActivity, InteractsWithActivity;
109+ }
110+ ```
111+
112+ With the ` InteractsWithActivity ` trait, you can easily query a model's activities:
113+
114+ ``` php
115+ $users = User::with('activities')->get();
116+
117+ // Access activities for a specific model
118+ $userActivities = $user->activities()->get();
119+ $userActivities = $user->activities()->where('event', 'updated')->get();
120+ ```
121+
122+ The ` InteractsWithActivity ` trait is optional and only required if you want to access ` $model->activities() ` .
123+
85124## Per-Model Event Control
125+
86126``` php
87- use Gottvergessen\Logger \Traits\TracksModelActivity;
127+ use Gottvergessen\Activity \Traits\TracksModelActivity;
88128
89129class User extends Authenticatable
90130{
@@ -96,29 +136,63 @@ class User extends Authenticatable
96136 ];
97137
98138 /*
99- * Limit which events are tracked:
100- *
139+ * Limit which events are tracked for this model
101140 */
102141}
103142```
104- ### Custom description per Model
143+ ## Advanced Usage
144+
145+ ### Custom Log Name per Model
146+
105147``` php
148+ use Gottvergessen\Activity\Traits\TracksModelActivity;
149+
150+ class Invoice extends Model
151+ {
152+ use TracksModelActivity;
153+
154+ public function activityLog(): string
155+ {
156+ return 'invoices';
157+ }
158+ }
159+ ```
160+
161+ ### Custom Action per Model
162+
163+ ``` php
164+ use Gottvergessen\Activity\Traits\TracksModelActivity;
165+
106166class Appointment extends Model
107167{
108168 use TracksModelActivity;
109169
110- public function activityDescription (string $event): string
170+ public function activityAction (string $event): string
111171 {
112172 return match ($event) {
113- 'created' => "Appointment scheduled for {$this->scheduled_at}",
114- 'updated' => "Appointment updated for {$this->scheduled_at}",
115- 'deleted' => "Appointment cancelled",
116- default => "Appointment {$event}",
173+ 'created' => "Appointment was scheduled for {$this->scheduled_at}",
174+ 'updated' => "Appointment has been updated to {$this->new_appointment_date}",
175+ default => $event,
117176 };
118177 }
119178}
120179```
121180
181+ ### Batch Operations
182+
183+ Feature not yet Implement but it will be:
184+ Group multiple model changes under a single batch ID:
185+
186+ ``` php
187+ use Gottvergessen\Activity\Activity;
188+
189+ Activity::batch(function () {
190+ $user->update(['name' => 'John']);
191+ $user->profile()->update(['bio' => 'Updated bio']);
192+ // Both changes share the same batch_id
193+ });
194+ ```
195+
122196
123197
124198## Contributing
0 commit comments