Download the PHP package axn/laravel-eloquent-authorable without Composer
On this page you can find all versions of the php package axn/laravel-eloquent-authorable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download axn/laravel-eloquent-authorable
More information about axn/laravel-eloquent-authorable
Files in axn/laravel-eloquent-authorable
Package laravel-eloquent-authorable
Short Description Support for created_by and updated_by fields in Eloquent models.
License MIT
Homepage https://github.com/AXN-Informatique/laravel-eloquent-authorable
Informations about the package laravel-eloquent-authorable
Laravel Eloquent Authorable
As Laravel Eloquent is able to automatically fill in the created_at
andupdated_at
fields,
this package provides automatic support for the created_by
andupdated_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.
So you can easily store and display this kind of information:
Added by AXN on 2016-03-24 and updated by forxer on 2020-10-01
Installation
With Composer :
Usage
To add functionality to a model, it is necessary that:
- The related table has the concerned fields (by default
created_at
andupdated_at
) - The model use the trait
Axn\EloquentAuthorable\Authorable
Database columns
You must create the columns in the database on the table of the model concerned. These columns are used to create the relationship between the related table and the "users" table.
For example :
Migrations helpers
Convenient utilities are available for adding or removing these columns in your migrations:
By default the addAuthorableColumns()
method will generate unsignedBigInteger columns type, if you need unsignedInteger instead you can pass the first parameter to false
.
Also you can pass a users model class name as third parameter if needed.
Warning ! These utilities use the columns names specified in the package configuration file at the time the migrations are run. If you modify this configuration, before or after having migrated, or if you overload it with the configuration by model, you should not use these utilities but add the columns by yourself. So these utilities are perfects for new application, but for old ones or for existing models, it is also recommended to create the columns yourself.
Important note You can customize both the column names and the users table through settings; and this globally or by model (see below)
Eloquent Model
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:
- createdBy()
- updatedBy()
Using Eloquent eager-loading
Using in Blade view
Settings
There are two ways to set this feature:
- globally for your application thanks to the configuration file
- or for each model that uses it
Global configuration
First initialise the config file in your application by running this command:
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'
Settings by model
Model and guard
By default, the user model App\User::class
and web
guard are used.
You can specify different ones like this:
Column names
By default, the created_by
andupdated_by
columns are used.
You can specify different column names for a model like this:
Enabling/Disabling
You can disable the feature like this: