kerattila/laravel-track-author

扩展,用于向 Eloquent 模型添加 created_by、updated_by 和 deleted_by 列。

v2.0.1 2023-04-11 18:01 UTC

This package is auto-updated.

Last update: 2024-09-27 07:59:01 UTC


README

此包允许您跟踪创建、删除和删除 eloquent 模型的用户。

安装和配置

您可以通过 composer 安装此包

composer require kerattila/laravel-track-author

运行以下命令发布配置文件

php artisan vendor:publish --provider="Kerattila\TrackAuthor\TrackAuthorServiceProvider"

发布配置文件后,根据您的需求调整值

<?php

return [
    'models' => [
        'user' => \App\User::class
    ],
    'columns' => [
        'createdByColumnName' => 'created_by',
        'updatedByColumnName' => 'updated_by',
        'deletedByColumnName' => 'deleted_by',
    ]
];

模型和迁移配置

迁移

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

Schema::table('posts', function (Blueprint $table) {
    // this will automatically add created_by, updated_by and updated_by nullable columns
    $table->trackAuthor();
});
            

模型

您可以将 Kerattila\TrackAuthor\Traits\TrackAuthor 特性添加到您的模型中,或者只需添加其中一个 CreatedBy、UpdatedBy 或 DeletedBy 特性。

示例

<?php

namespace App;

// use Kerattila\TrackAuthor\Traits\CreatedBy;
// use Kerattila\TrackAuthor\Traits\UpdatedBy;
// use Kerattila\TrackAuthor\Traits\DeletedBy;
use Kerattila\TrackAuthor\Traits\TrackAuthor;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use TrackAuthor;
    // use CreatedBy, UpdatedBy, DeletedBy;
}

关系

关系是 BelongsTo 类型,可以通过以下方式访问

$post = \App\Post::find(1);
$createdBy = $post->createdBy;
$updatedBy = $post->updatedBy;
$deletedBy = $post->deletedBy;