rulr/laravel-authored
最新稳定版本:v1.2.1
Composer 安装命令:
composer require rulr/laravel-authored
包简介
A Laravel package that adds created_by and last_updated_by attributes to Eloquent models.
README 文档
README
Introduction
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.
Installation
You can install the package via Composer:
composer require rulr/laravel-authored
Usage
1. Apply the HasAuthor Trait
To 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; }
2. Use the authored Macro in Migrations
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(); });
3. Configuration
By default, this package uses created_by and updated_by columns. You can customize these field names by publishing and modifying the config file.
Publishing the Config File
php artisan vendor:publish --provider="Rulr\Authored\AuthoredServiceProvider" --tag=config
This 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.
4. Automatically Set User IDs
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;
Testing
This package includes PHPUnit tests using Orchestra Testbench. The tests create tables dynamically without requiring migrations. To run tests, use:
vendor/bin/phpunit
License
This package is open-source software licensed under the MIT license.
统计信息
- 总下载量: 1.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-06