https://github.com/hexadog/laravel-auditable
Know who manipulates your models in your Laravel application.
https://github.com/hexadog/laravel-auditable
Last synced: about 1 year ago
JSON representation
Know who manipulates your models in your Laravel application.
- Host: GitHub
- URL: https://github.com/hexadog/laravel-auditable
- Owner: hexadog
- License: mit
- Created: 2021-05-12T19:07:45.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-10-23T13:43:36.000Z (over 2 years ago)
- Last Synced: 2024-03-25T03:46:04.225Z (about 2 years ago)
- Language: PHP
- Size: 195 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README

hexadog/laravel-auditable helps you to automatically register the user making action on your models.
## Installation
This package requires PHP 7.3 and Laravel 7.0 or higher.
To get started, install Auditable using Composer:
```shell
composer require hexadog/laravel-auditable
```
The package will automatically register its service provider.
## Usage
Use the new `auditable` macro into your migrations.
```php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('posts', function (Blueprint $table) {
$table->auditable();
});
Schema::table('posts', function (Blueprint $table) {
$table->dropAuditable();
});
```
It will add the following columns:
- created_at
- created_by
- updated_at
- updated_by
- deleted_at
- deleted_by
**Notice:** You don't have to use `timestamps()` nor `sofDeletes()` macros. Auditable macro will integrate them for you.
_If you altered you DB and want to drop auditable columns you can use the macro `$table->dropAuditable()`;_
Once database has been migrated you can use the `Auditable` trait into your model.
```php
use Hexadog\Auditable\Models\Traits\Auditable;
class Post extends Models
{
use Auditable;
// ...
}
```
This way the id of the user responsible of the action (creation, update, soft deletion) is automatically registered into your model in the associated column each time the data is touched. You don't have to do anything.
## Retreive user responsible of the action
You can retreive the user respoinsible of the last action (create, update, delete) by calling one of the helper methods provided by the trait.
To determine the user responsible of the model creation you may use the `created_by` attribute to get the id or the `createdBy` relation to get the target model:
```php
// Get user responsible of the creation of the post
$post->createdBy;
// Get the creation date
$post->created_at;
```
To determine the user responsible of the last model update you may use the `updated_by` attribute to get the id or the `updatedBy` relation to get the target model:
```php
// Get user responsible of the last update of the post
$post->updatedBy;
// Get the last update date
$post->updated_at;
```
To determine the user responsible of the model deletion (only if the model uses `SoftDeletes` trait) you may use the `deleted_by` attribute to get the id or the `deletedBy` relation to get the target model:
```php
// Get user responsible of the post deletion (ONLY if soft deletes are used)
$post->deletedBy;
// Get the soft deletion date
$post->deleted_at;
```
## Credits
- Logo made by [BeyondCode](https://banners.beyondco.de/)
## License
Laravel Auditable is open-sourced software licensed under the [MIT license](LICENSE).