rulr/laravel-authored is a Laravel package that automatically assigns created_by and updated_by user IDs to Eloquent models. This helps track which user created and last updated a model, improving data auditing and accountability.
You can install the package via Composer:
composer require rulr/laravel-authoredTo enable automatic tracking, add the HasAuthor trait to your Eloquent models:
use Illuminate\Database\Eloquent\Model;
use Rulr\Authored\Traits\HasAuthor;
class Post extends Model
{
use HasAuthor;
}Instead of manually defining created_by and updated_by, you can use the provided macro when defining your schema:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->authored(); // Automatically adds created_by and updated_by fields
$table->timestamps();
});By default, this package uses created_by and updated_by columns. You can customize these field names by publishing and modifying the config file.
php artisan vendor:publish --provider="Rulr\Authored\AuthoredServiceProvider" --tag=configThis will create a file at config/authored.php, where you can modify the column names:
return [
'created_by' => 'created_by',
'updated_by' => 'updated_by',
];Now, when using $table->authored();, it will use the specified field names.
When a user creates or updates a model, Laravel will automatically assign their ID:
$post = Post::create(['title' => 'First Post']);
$post->update(['title' => 'Updated Title']);
// Outputs the user ID who created the post
echo $post->created_by;
// Outputs the user ID who last updated the post
echo $post->updated_by;This package includes PHPUnit tests using Orchestra Testbench. The tests create tables dynamically without requiring migrations. To run tests, use:
vendor/bin/phpunitThis package is open-source software licensed under the MIT license.