Skip to content

Commit 6760270

Browse files
committed
Fix #163: optionally include the model related to the activity.
1 parent fad77d4 commit 6760270

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Laravel logger is an activity event logger for your Laravel or Lumen application
3939
|Records activity timestamps|
4040
|Records activity description|
4141
|Records activity details (optional)|
42-
|Records activity user type with crawler detection.|
42+
|Records model related to the activity (optional)|
43+
|Records activity user type with crawler detection|
4344
|Records activity Method|
4445
|Records activity Route|
4546
|Records activity Ip Address|
@@ -233,7 +234,7 @@ To use the trait:
233234
use ActivityLogger;
234235
```
235236

236-
3. You can record the activity my calling the traits method:
237+
3. You can record the activity by calling the traits method:
237238
```
238239
ActivityLogger::activity("Logging this activity.");
239240
```
@@ -243,6 +244,11 @@ To use the trait:
243244
ActivityLogger::activity("Logging this activity.", "Additional activity details.");
244245
```
245246

247+
Or even including the model related to the activity:
248+
```
249+
ActivityLogger::activity("Logging this activity.", "Additional activity details.", ["id" => 1, "model" => "App\Models\User"]);
250+
```
251+
246252
### Routes
247253
##### Laravel Activity Dashbaord Routes
248254

src/App/Http/Traits/ActivityLogger.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ trait ActivityLogger
1515
*
1616
* @param null $description
1717
* @param null $details
18+
* @param ?array $rel
1819
*
1920
* @return void
2021
*/
21-
public function activity($description = null, $details = null)
22+
public function activity($description = null, $details = null, array $rel = null)
2223
{
2324
$userType = trans('LaravelLogger::laravel-logger.userTypes.guest');
2425
$userId = null;
@@ -68,6 +69,13 @@ public function activity($description = null, $details = null)
6869
$ip = Request::ip();
6970
}
7071

72+
$relId = null;
73+
$relModel = null;
74+
if (is_array($rel) && array_key_exists('id', $rel) && array_key_exists('model', $rel)) {
75+
$relId = $rel['id'];
76+
$relModel = $rel['model'];
77+
}
78+
7179
$data = [
7280
'description' => $description,
7381
'details' => $details,
@@ -79,6 +87,8 @@ public function activity($description = null, $details = null)
7987
'locale' => Request::header('accept-language'),
8088
'referer' => Request::header('referer'),
8189
'methodType' => Request::method(),
90+
'relId' => $relId,
91+
'relModel' => $relModel,
8292
];
8393

8494
// Validation Instance
@@ -113,6 +123,8 @@ private static function storeActivity($data)
113123
'locale' => $data['locale'],
114124
'referer' => $data['referer'],
115125
'methodType' => $data['methodType'],
126+
'relId' => $data['relId'],
127+
'relModel' => $data['relModel'],
116128
]);
117129
}
118130

src/database/migrations/2017_11_04_103444_create_laravel_logger_activity_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function up()
3232
$table->string('locale')->nullable();
3333
$table->longText('referer')->nullable();
3434
$table->string('methodType')->nullable();
35+
$table->unsignedBigInteger('relId')->index()->nullable();
36+
$table->string('relModel')->nullable();
3537
$table->timestamps();
3638
$table->softDeletes();
3739
});

0 commit comments

Comments
 (0)