papski
papski

Reputation: 1281

How to implement owen-it/laravel-auditing in a model

I want to create an audit trail in my model. I already installed owen-it/laravel-auditing package via Composer. My question is that how can I implement it in my Model or controller. Please see my code for controller and Model below. Thanks

My Controller :

<?php

namespace App\Http\Controllers;

use App\Events\Test;
use App\Letter;
use App\Notifications\LetterNotification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Validator;

class LetterController extends Controller
{
    public function viewLetter()
    {
        return view('welcome');
    }

    /**
     * Saves email into database
     *
     * @param  array  $data
     * @return Letter
     */
    protected function create(array $data)
    {
        $letter = Letter::create([
            'email' => $data['email']
        ]);
        $this->letterNotify($letter);

        return $letter;
    }

    /**
     * Validates email
     */
    public function createLetter(Request $request)
    {
        $this->validate($request,[
            'email' => 'required|email|max:255|unique:letters'
        ],[
            'email.required' => 'Email is required.',
            'email.unique' => 'Already registered.',
            'email.email' => 'Please put a valid Email address'
        ]);

        $this->create($request->all());

        return redirect('/')->with('info','You are now registered.');
    }

    protected function letterNotify($letter)
    {`enter code here`
        Notification::send($letter, new LetterNotification($letter));
    }


}

For my Model:

<?php

namespace App;

use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Letter extends Model implements AuditableContract
{
    use Notifiable;

    use Auditable;

    protected $fillable = ['email'];

    protected $table = 'letters';
}

Upvotes: 0

Views: 4455

Answers (1)

Quetzy Garcia
Quetzy Garcia

Reputation: 1840

Like I stated in my comment, the Laravel Auditing package only triggers an audit on a database operation involving an Eloquent model and event (by default, created, updated, deleted, restored).

Having said that, here's a list of steps to create an audit when logging in/out:

  • Create a listener for the Illuminate\Auth\Events\Login event;
  • Once fired, update a column in the users table that keeps track of the latest login date/time (latest_login_at, for example);
  • (Optional) update a column with the previous login date/time (last_login_at, for example);
  • By doing those updates to the users table, the Auditor kicks in;
  • You can also listen for the OwenIt\Auditing\Events\Auditing or OwenIt\Auditing\Events\Audited events and apply more logic if needed;
  • Follow the same steps for the Illuminate\Auth\Events\Logout event;

Upvotes: 2

Related Questions