-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_integration.php
More file actions
71 lines (56 loc) · 1.81 KB
/
Copy pathmodel_integration.php
File metadata and controls
71 lines (56 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Dibakar\ActivityScope\Traits\HasActivities;
use Dibakar\ActivityScope\Traits\LogsActivity;
/**
* Example: Post Model
*
* Demonstrates proper usage of:
* - LogsActivity: For automatic event logging (created, updated, deleted)
* - HasActivities: For relationship access (model->actions, model->activities)
*/
class Post extends Model
{
// Auto-logs 'created', 'updated', 'deleted' events
use LogsActivity;
// Provides relationships: actions(), activities()
use HasActivities;
protected $fillable = ['title', 'content', 'status'];
}
/**
* Example: User Model
*
* Typically users perform actions (actors) but can also be subjects.
*/
class User extends Model
{
use HasActivities;
// ... user logic
}
// usage_example.php
$user = User::find(1);
$post = new Post(['title' => 'Hello World']);
// 1. Auto-logging in action
// This will automatically create a "created" activity
// Actor: Auth::user() (if logged in) or System
// Subject: The new Post instance
$post->save();
// 2. Accessing Logs via Relationships
// Get all activities performed ON this post
$history = $post->activities()->latest()->get();
foreach ($history as $activity) {
echo "{$activity->created_at}: {$activity->action} by {$activity->actor?->name}";
}
// 3. User History
// Get all activities performed BY this user
$userActions = $user->actions()->get();
// 4. Manual Activity using Model Context
// Using newActivity() from HasActivities trait automatically sets:
// - The model as the Subject (if it implements ActivitySubject)
// - The model as the Actor (if it implements ActivityActor AND calls it)
// But usually, you want independent logging:
$post->newActivity()
->did('published')
->with(['scheduled_for' => '2023-12-25'])
->log();