You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a model for the pivot table that extends Laravel's Pivot class. This class must use the AuditLoggablePivot trait and have a defined `$audit_loggable_keys` variable, which is used to map the pivot to the audit log table.
87
+
88
+
```php
89
+
class PostTag extends Pivot
90
+
{
91
+
use AuditLoggablePivot;
92
+
93
+
/**
94
+
* The array keys are the composite key in the audit log
95
+
* table while the pivot table columns are the values.
96
+
*
97
+
* @var array
98
+
*/
99
+
protected $audit_loggable_keys = [
100
+
'post_id' => 'post_id',
101
+
'tag_id' => 'tag_id',
102
+
];
103
+
}
104
+
```
105
+
Side note: if a column shares the same name in the pivot and a column already in the audit log table (ex: `user_id`), change the name of the column in the audit log table (ex: `audit_user_id`) and define the relationship as `'audit_user_id' => 'user_id'`.
106
+
107
+
The two models that are joined by the pivot will need to be updated so that events fire on the pivot model. Currently Laravel doesn't support pivot events so a third party package is required.
108
+
```php
109
+
composer require fico7489/laravel-pivot
110
+
```
111
+
112
+
Have both models use the PivotEventTrait
113
+
```php
114
+
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
115
+
use Illuminate\Database\Eloquent\Model;
116
+
117
+
class Post extends Model
118
+
{
119
+
use PivotEventTrait;
120
+
```
121
+
122
+
Modify the belongsToMany join on both related models to include the using function along with the pivot model.
123
+
In the Post model:
124
+
```php
125
+
public function tags()
126
+
{
127
+
return $this->belongsToMany(Tag::class)
128
+
->using(PostTag::class);
129
+
}
130
+
```
131
+
In the Tag model:
132
+
```php
133
+
public function posts()
134
+
{
135
+
return $this->belongsToMany(Post::class)
136
+
->using(PostTag::class);
137
+
}
138
+
```
139
+
140
+
When a pivot record is deleted through `detach` or `sync`, an audit log record for each of the keys (ex: `post_id` and `tag_id`) will added to the audit log table. The `field_value_old` will be the id of the record and the `field_value_new` will be null. The records will have an event type of `PIVOT_DELETED` (id: 6).
141
+
142
+
If you need to pull the audit logs through the `auditLogs` relationship (ex: $post_tag->auditLogs()->get()), support for composite keys is required.
143
+
```php
144
+
composer require awobaz/compoships
145
+
```
146
+
Then use the trait on the pivot audit log model:
147
+
```php
148
+
use Awobaz\Compoships\Compoships;
149
+
use OrisIntel\AuditLog\Models\BaseModel;
150
+
151
+
class PostTagAuditLog extends BaseModel
152
+
{
153
+
use Compoships;
154
+
```
155
+
156
+
For a working example of pivots with the audit log, see `laravel-model-auditlog/tests/Fakes`, which contains working migrations and models.
0 commit comments