PHPackages                             axn/laravel-eloquent-authorable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. axn/laravel-eloquent-authorable

ActiveLibrary[Database &amp; ORM](/categories/database)

axn/laravel-eloquent-authorable
===============================

Support for created\_by and updated\_by fields in Eloquent models.

7.1.0(3mo ago)342.7k↑15.1%3[1 issues](https://github.com/AXN-Informatique/laravel-eloquent-authorable/issues)[1 PRs](https://github.com/AXN-Informatique/laravel-eloquent-authorable/pulls)1MITPHPPHP ^8.4

Since Mar 24Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/AXN-Informatique/laravel-eloquent-authorable)[ Packagist](https://packagist.org/packages/axn/laravel-eloquent-authorable)[ Docs](https://github.com/AXN-Informatique/laravel-eloquent-authorable)[ RSS](/packages/axn-laravel-eloquent-authorable/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (8)Versions (27)Used By (1)

Laravel Eloquent Authorable
===========================

[](#laravel-eloquent-authorable)

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.

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
------------

[](#installation)

With Composer :

```
composer require axn/laravel-eloquent-authorable
```

Usage
-----

[](#usage)

To add functionality to a model, it is necessary that:

1. The related table has the concerned fields (by default `created_at` and `updated_at`)
2. The model use the trait `Axn\EloquentAuthorable\Authorable`

### Database columns

[](#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 :

```
Schema::table('posts', function (Blueprint $table) {
    $table->unsignedInteger('created_by')->nullable();
    $table->unsignedInteger('updated_by')->nullable();

    $table->foreign('created_by')
        ->references('id')
        ->on('users');

    $table->foreign('updated_by')
        ->references('id')
        ->on('users');
});
```

#### Migrations helpers

[](#migrations-helpers)

Convenient utilities are available for adding or removing these columns in your migrations:

```
Schema::create('posts', function (Blueprint $table) {
    //...
    $table->addAuthorableColumns();
});
```

```
Schema::table('posts', function (Blueprint $table) {
    //...
    $table->dropAuthorableColumns();
});
```

By default the `addAuthorableColumns()` method will generate unsignedBigInteger columns type, if you need unsignedInteger instead you can pass the first parameter to `false`.

```
Schema::create('posts', function (Blueprint $table) {
    //...
    $table->addAuthorableColumns(false);
});
```

Also you can pass a users model class name as third parameter if needed.

```
Schema::create('posts', function (Blueprint $table) {
    //...
    $table->addAuthorableColumns(true, App\Models\User::class);
});
```

**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

[](#eloquent-model)

```
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    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:

- createdBy()
- updatedBy()

#### Using Eloquent eager-loading

[](#using-eloquent-eager-loading)

```
$post = Post::with('createdBy', 'updatedBy')->first();
```

### Using in Blade view

[](#using-in-blade-view)

```
Created by {{ $post->createdBy->name }} ({{ $post->createdBy->email }})
and updated by {{ $post->updatedBy->name }} ({{ $post->updatedBy->email }})
```

Settings
--------

[](#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

[](#global-configuration)

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'`

### Settings by model

[](#settings-by-model)

#### Model and guard

[](#model-and-guard)

By default, the user model `App\User::class` and `web` guard are used.

You can specify different ones like this:

```
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'users_model' => \App\Admin::class,
        'guard' => 'admin',
    ];

    //...
}
```

#### Column names

[](#column-names)

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\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'created_by_column_name' => 'custom_created_by',
        'updated_by_column_name' => 'custom_updated_by',
    ];

    //...
}
```

#### Enabling/Disabling

[](#enablingdisabling)

You can disable the feature like this:

```
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'set_author_when_creating' => false,
        'set_author_when_updating' => false,
    ];

    //...
}
```

#### Full example of custom implementation

[](#full-example-of-custom-implementation)

```
use Axn\EloquentAuthorable\AuthorableTrait;
use Illuminate\Database\Eloquent\Model

class Post extends Model
{
    use AuthorableTrait;

    public $authorable = [
        'users_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,
    ];

    //...
}
```

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance74

Regular maintenance activity

Popularity33

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity91

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 76.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~152 days

Recently: every ~281 days

Total

25

Last Release

103d ago

Major Versions

2.1.0 → 3.0.02017-08-31

3.2.1 → 4.0.02019-12-31

4.2.0 → 5.0.02020-10-01

5.1.0 → 6.0.02021-10-21

6.x-dev → 7.0.02025-03-20

PHP version history (2 changes)6.0.0PHP ^8.0

7.0.0PHP ^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1314974?v=4)[Axel Neumann](/maintainers/axn)[@axn](https://github.com/axn)

---

Top Contributors

[![forxer](https://avatars.githubusercontent.com/u/407917?v=4)](https://github.com/forxer "forxer (35 commits)")[![lgtaxn](https://avatars.githubusercontent.com/u/23211553?v=4)](https://github.com/lgtaxn "lgtaxn (6 commits)")[![sylvainR37](https://avatars.githubusercontent.com/u/29980311?v=4)](https://github.com/sylvainR37 "sylvainR37 (3 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![martijngastkemper](https://avatars.githubusercontent.com/u/250662?v=4)](https://github.com/martijngastkemper "martijngastkemper (1 commits)")

###  Code Quality

Static AnalysisRector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/axn-laravel-eloquent-authorable/health.svg)

```
[![Health](https://phpackages.com/badges/axn-laravel-eloquent-authorable/health.svg)](https://phpackages.com/packages/axn-laravel-eloquent-authorable)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11223.5M33](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3417.0k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
