Package Data | |
---|---|
Maintainer Username: | axn |
Maintainer Contact: | developpement@axn.fr (AXN Informatique) |
Package Create Date: | 2016-11-02 |
Package Last Update: | 2024-02-27 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:17:33 |
Package Statistics | |
---|---|
Total Downloads: | 39,709 |
Monthly Downloads: | 495 |
Daily Downloads: | 10 |
Total Stars: | 2 |
Total Watchers: | 3 |
Total Forks: | 2 |
Total Open Issues: | 2 |
As Laravel Eloquent is able to automatically fill in the created_at
and updated_at
fields,
this package provides automatic support for the created_by
and updated_by
fields in your Eloquent models.
This package will avoid you to always indicate when creating and/or updating a model who is the user who performed this action. This package does it for you and it simplifies the recovery of this information.
| Laravel | Package | | -------- | ------- | | 5.8.x | 3.x | | 5.7.x | 3.x | | 5.6.x | 3.x | | 5.5.x | 3.x | | 5.4.x | 3.x | | 5.3.x | 2.x | | 5.2.x | 2.x | | 5.1.x | 1.x | | 5.0.x | 1.x |
With Composer :
composer require axn/laravel-eloquent-authorable
In Laravel 5.5 the service provider is automaticaly included.
In older versions of the framework, simply add this service provider to the array
of providers in config/app.php
:
// config/app.php
'provider' => [
//...
Axn\EloquentAuthorable\ServiceProvider::class,
//...
];
To add functionality to a model, it is necessary that:
Axn\EloquentAuthorable\Authorable
Axn\EloquentAuthorable\Authorable
created_at
and updated_at
)
use Axn\EloquentAuthorable\Authorable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model
class Post extends Model implements Authorable
{
use AuthorableTrait;
//...
}
From now on, each creation/update of an entry in the Posts
table
the created_by
and updated_by
columns will automatically be filled
with the id of the currently authenticated user.
In addition two 1-n inverse relationships (belongs to) with the users table are available:
Created by {{ $post->createdBy->name }} ({{ $post->createdBy->email }})
and updated by {{ $post->updatedBy->name }} ({{ $post->updatedBy->email }})
$post = Post::with('createdBy', 'updatedBy')->first();
There are two ways to set this feature:
First initialise the config file in your application by running this command:
php artisan vendor:publish --provider="Axn\EloquentAuthorable\ServiceProvider" --tag="config"
Then, when published, the config/eloquent-authorable.php
config file will contain the default values that you can then customize.
Default values in this file are:
users_model
: App\User::class
guard
: web
set_author_when_creating
: true
set_author_when_updating
: true
created_by_column_name
: 'created_by'
updated_by_column_name
: 'updated_by'
By default, the user model App\User::class
and web
guard are used.
You can specify different ones like this:
use Axn\EloquentAuthorable\Authorable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model
class Post extends Model implements Authorable
{
use AuthorableTrait;
public $authorable = [
'model' => \App\Admin::class,
'guard' => 'admin',
];
//...
}
By default, the created_by
and updated_by
columns are used.
You can specify different column names for a model like this:
use Axn\EloquentAuthorable\Authorable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model
class Post extends Model implements Authorable
{
use AuthorableTrait;
public $authorable = [
'created_by_column_name' => 'custom_created_by',
'updated_by_column_name' => 'custom_updated_by',
];
//...
}
You can disable the feature like this:
use Axn\EloquentAuthorable\Authorable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model
class Post extends Model implements Authorable
{
use AuthorableTrait;
public $authorable = [
'set_author_when_creating' => false,
'set_author_when_updating' => false,
];
//...
}
use Axn\EloquentAuthorable\Authorable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model
class Post extends Model implements Authorable
{
use AuthorableTrait;
public $authorable = [
'model' => \App\Admin::class,
'guard' => 'admin',
'created_by_column_name => 'custom_created_by',
'set_author_when_creating' => true,
'updated_by_column_name' => 'custom_updated_by',
'set_author_when_updating' => false,
];
//...
}