Pharaonic
Loading...

Getting Started

Auditable

# Why Auditable?

Auditable is a package that helps to trace the create, update and delete actions and when it's done on your Laravel Eloquent/Model.

# Installation

Install the latest version using Composer.

1composer require pharaonic/laravel-auditable

# Usage

You have to include Auditable in your migration and model first, then everything will be automated.

# Inclusion in migration

This is what you should to include in your migration file to create auditable columns, just use one of this 2 ways.

1// created_by, created_at
2// updated_by, updated_at
3$table->auditable();
4 
5// created_by, created_at
6// updated_by, updated_at
7// deleted_by, deleted_at
8$table->auditableWithSoftDeletes();

# Inclusion in model

This is what you should to include in your model file.

1namespace App\Models;
2 
3use Illuminate\Database\Eloquent\Model;
4use Pharaonic\Laravel\Audits\Auditable;
5 
6class Article extends Model
7{
8 use Auditable;
9}

# Getting auditor and datetime.

This is how you can get the auditor who create/update/delete the record and when did it.

1// Creating
2$article = Article::create(['title' => 'Moamen Eltouny']);
3echo $article->created_at->isoFormat('LLLL');
4echo $article->created_by->name;
5 
6// Updating
7$article = Article::first();
8echo $article->updated_at->isoFormat('LLLL');
9echo $article->updated_by->name;
10 
11// Deleting (ONLY WITH SoftDeletes)
12$article = Article::first();
13$article->delete();
14echo $article->deleted_at->isoFormat('LLLL');
15echo $article->deleted_by->name;

# Relationships

  • createdBy
  • updatedBy
  • deletedBy

# Contributors

MoamenEltouny
14 Contributions