Skip to content

Commit f9f7d3d

Browse files
author
Tom Schlick
authored
support ignoring fields at the model level (#7)
* support ignoring fields at the model level * add example for more custom exclude logic
1 parent 466d391 commit f9f7d3d

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ php artisan make:model-auditlog "\App\User"
4242

4343
Replace `\App\User` with your own model name. Model / table options can be tweaked in the config file.
4444

45+
If you need to ignore specific fields on your model, extend the `getAuditLogIgnoredFields()` method and return an array of fields.
46+
47+
```php
48+
public function getAuditLogIgnoredFields() : array
49+
{
50+
return ['posted_at'];
51+
}
52+
```
53+
54+
Using that functionality, you can add more custom logic around what should be logged. An example might be to not log the title changes of a post if the post has not been published yet.
55+
```php
56+
public function getAuditLogIgnoredFields() : array
57+
{
58+
if ($this->postHasBeenPublished()) {
59+
return ['title'];
60+
}
61+
62+
return [];
63+
}
64+
```
65+
4566
### Testing
4667

4768
``` bash

src/Models/BaseModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static function recordChanges(int $event_type, $model) : void
3838

3939
collect($changes)
4040
->except(config('model-auditlog.global_ignored_fields'))
41+
->except($model->getAuditLogIgnoredFields())
4142
->except([
4243
$model->getKeyName(), // Ignore the current model's primary key
4344
'created_at',

src/Traits/AuditLoggable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function getAuditLogTableName() : string
4343
return $this->getTable() . config('model-auditlog.table_suffix');
4444
}
4545

46+
/**
47+
* Get fields that should be ignored from the auditlog for this model.
48+
*
49+
* @return array
50+
*/
51+
public function getAuditLogIgnoredFields() : array
52+
{
53+
return [];
54+
}
55+
4656
/**
4757
* Get the audit logs for this model.
4858
*
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace OrisIntel\AuditLog\Tests\Fakes\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use OrisIntel\AuditLog\Traits\AuditLoggable;
7+
8+
class IgnoredFieldsPost extends Model
9+
{
10+
use AuditLoggable;
11+
12+
protected $guarded = [];
13+
14+
protected $table = 'posts';
15+
16+
public function getAuditLogIgnoredFields() : array
17+
{
18+
return ['posted_at'];
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace OrisIntel\AuditLog\Tests\Fakes\Models;
4+
5+
use OrisIntel\AuditLog\Models\BaseModel;
6+
7+
class IgnoredFieldsPostAuditLog extends BaseModel
8+
{
9+
public $timestamps = false;
10+
11+
public $table = 'posts_auditlog';
12+
}

tests/PostModelTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Testing\DatabaseTransactions;
66
use Illuminate\Support\Collection;
77
use OrisIntel\AuditLog\EventType;
8+
use OrisIntel\AuditLog\Tests\Fakes\Models\IgnoredFieldsPost;
89
use OrisIntel\AuditLog\Tests\Fakes\Models\NonSoftDeletePost;
910
use OrisIntel\AuditLog\Tests\Fakes\Models\Post;
1011
use OrisIntel\AuditLog\Tests\Fakes\Models\PostAuditLog;
@@ -148,4 +149,20 @@ public function restoring_a_post_triggers_a_revision()
148149
$this->assertEquals('deleted_at', $last->field_name);
149150
$this->assertNull($last->field_value_new);
150151
}
152+
153+
/** @test */
154+
public function fields_can_be_ignored()
155+
{
156+
/** @var Post $post */
157+
$post = IgnoredFieldsPost::create([
158+
'title' => 'Test',
159+
'posted_at' => '2019-04-05 12:00:00',
160+
]);
161+
162+
$this->assertEquals(1, $post->auditLogs()->count());
163+
164+
$post->update(['posted_at' => now()]);
165+
166+
$this->assertEquals(1, $post->auditLogs()->count());
167+
}
151168
}

0 commit comments

Comments
 (0)