Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisgrim's avatar

Question about Laravel Auditing plugin

Hi, I am using the Laravel Auditing app https://github.com/owen-it/laravel-auditing to track changes on my events. However, I was hoping to set it up so that it would only track changes if the event is published. In my model if the event.status == 'p' that means it's published. Right now it tracks every single event, even those that aren't published.

0 likes
6 replies
bobbybouwmann's avatar

I don't see such functionality in that package.

You can implement this yourself by disabling and enabling the auditing based on the status of your event

Event::disableAuditing();

if ($event->status === 'p') {
    Event::enableAuditing();
}

Event::disableAuditing();

This is only useful if you do it one place and not if you need to use this on multiple locations in your application.

Documentation: https://github.com/owen-it/laravel-auditing-doc/blob/master/auditable-configuration.md#enabledisable

The best solution is probably writing your own audit driver. In that driver, you can do something like this

class MyCustomDriver extends \OwenIt\Auditing\Drivers\Database
{
    public function audit(Auditable $model): Audit
    {
        if ($model instanceof Event && $model->status === 'p') {
            return;
        }

        // Do the regular implementation
        return parent::audit($model);
    }
}

Documentation: https://github.com/owen-it/laravel-auditing-doc/blob/master/audit-drivers.md

2 likes
chrisgrim's avatar

Hi @bobbybouwmann Thanks so much for taking the time to help me with this. I definitely need to create my own audit driver. I tried following the instructions. I created the MyCustomDriver.php in App/AuditDrivers and in that file I have

<?php
namespace App\AuditDrivers;

use OwenIt\Auditing\Contracts\Audit;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Contracts\AuditDriver;
use App\Event;

class MyCustomDriver implements AuditDriver
{
    /**
     * Perform an audit.
     *
     * @param \OwenIt\Auditing\Contracts\Auditable $model
     *
     * @return \OwenIt\Auditing\Contracts\Audit
     */
    public function audit(Auditable $model): Audit
    {
        if ($model instanceof Event && $model->status === 'p') {
            return;
        }

        // Do the regular implementation
        return parent::audit($model);
    }

    /**
     * Remove older audits that go over the threshold.
     *
     * @param \OwenIt\Auditing\Contracts\Auditable $model
     *
     * @return bool
     */
    public function prune(Auditable $model): bool
    {
        // TODO: Implement the pruning logic
    }
}

and in my config folder for audit.php I changed


    // 'driver' => 'database',
    'driver' => App\AuditDrivers\MyCustomDriver::class,

    /*
    |--------------------------------------------------------------------------
    | Audit Driver Configurations
    |--------------------------------------------------------------------------
    |
    | Available audit drivers and respective configurations.
    |
    */

    'drivers' => [
        'database' => [
            'table'      => 'audits',
            'connection' => null,
        ],
    ],

but when I try to save some data I get

A function with return type must return a value",…}
exception: "Symfony\Component\ErrorHandler\Error\FatalError"
file: "/Users/chrisgrim/code/ei/app/AuditDrivers/MyCustomDriver.php"
line: 20
message: "A function with return type must return a value"
trace: []

This is a bit beyond me but I am trying my best to understand

bobbybouwmann's avatar

Mmh, yeah that method always needs to return an Audit object. If you remove : Audit on the method it should work as expected.

1 like
chrisgrim's avatar

Hi @bobbybouwmann So I tried

public function audit(Auditable $model)
    {
        if ($model instanceof Event && $model->status === 'p') {
            return;
        }

        // Do the regular implementation
        return parent::audit($model);
    }

and it gave me

exception: "Symfony\Component\ErrorHandler\Error\FatalError"
file: "/Users/chrisgrim/code/ei/app/AuditDrivers/MyCustomDriver.php"
line: 18
message: "Declaration of App\AuditDrivers\MyCustomDriver::audit(OwenIt\Auditing\Contracts\Auditable $model) must be compatible with OwenIt\Auditing\Contracts\AuditDriver::audit(OwenIt\Auditing\Contracts\Auditable $model): OwenIt\Auditing\Contracts\Audit"
trace: []
bobbybouwmann's avatar

Mmh, yeah that won't work... Inheritance

The only other option I can think of is throwing an exception and catching it on the place where you create/update the event. This way you don't have issues with return types.

Maybe this package is not suited for your case, it sounds to me you need a custom solution for this.

1 like

Please or to participate in this conversation.