Skip to content
On this page

Custom audits

Since v13 you may audit actions that does not origin from eloquents 'created', 'updated', 'deleted', 'restored' and 'retrieved' events.

Lets say you have articles and categories

class Article extends Model implements \OwenIt\Auditing\Contracts\Auditable
    use Auditable;

    public function categories()
        return $this->morphToMany(Category::class, 'model', 'model_has_categories');

Migrations for this example looks like this:

Schema::create('categories', function (Blueprint $table) {

Schema::create('model_has_categories', function (Blueprint $table) {

Many things can be categorized, so we are using a morphToMany relation so that we can use $article->sync($category), $article->attach($category) or $article->detach($category). The common problem with this is, that no change is made on the Auditable (Article) or on the Category side, and thus we have no direct way to audit the assigned categories.

In version 13 of laravel-auditing, you can use Auditables attach, detach and sync pass-through methods like this: $article->auditAttach('categories', $category);. It works the same way as the Eloquent attach, but it registers a custom event and writes an audit of the changes as if it was an array property of article.

First argument to the auditAttach, auditDetach and auditSync methods is the name of the relationship. In the example above, that is "categories".

The audited value is the array version of the relationship return!

Example: Custom log event

You may custom-log any values you wish, as long as they are on an Auditable model. It works by dispatching the \OwenIt\Auditing\Events\AuditCustom event with the Auditable as argument when you have defined what to log

$article = Article::find(1);
$article->auditEvent = 'whateverYouWant';
$article->isCustomEvent = true;
$article->auditCustomOld = [
    'customExample' => 'Anakin Skywalker'
$article->auditCustomNew = [
    'customExample' => 'Darth Vader'
Event::dispatch(AuditCustom::class, [$article]);

Released under the MIT License.