Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Latest commit

 

History

History
54 lines (42 loc) · 1.52 KB

auditor.md

File metadata and controls

54 lines (42 loc) · 1.52 KB

Auditor

The Auditor class is responsible for auditing and clearing Audit records.

Usually, there's no need to use it directly, since the AuditableObserver takes care of things for us.

Yet, should the need to call it manually arise, here is how it could be done.

Using the Auditor Facade

<?php
namespace App\Http\Controllers;

use App\Http\Requests\Request;
use App\Models\Article;
use OwenIt\Auditing\Facades\Auditor;

class ArticleController extends Illuminate\Routing\Controller
{
    public function update(Request $request, Article $article)
    {
        $article->update($request->all());

        if ($audit = Auditor::execute($article)) {
            Auditor::prune($article);
        }
    }
}

Injecting the Auditor as a dependency

With the AuditingServiceProvider registered, we can use the IoC to resolve and inject the Auditor into a method.

<?php
namespace App\Http\Controllers;

use App\Http\Requests\Request;
use App\Models\Article;
use OwenIt\Auditing\Contracts\Auditor;

class ArticleController extends Illuminate\Routing\Controller
{
    public function update(Request $request, Article $article, Auditor $auditor)
    {
        $article->update($request->all());

        if ($audit = $auditor->execute($article)) {
            $auditor->prune($article);
        }
    }
}

{note} These are just examples on how to manually call the Auditor. Also keep in mind that, unless you set the model's $auditEvents property to an empty array, you'll get duplicate Audit records this way.