arkitecht/laravel-attributions

Laravel 5/6/7/8/9/10 - 跟踪归属(模型创建者/更新者)

5.2 2024-07-08 16:18 UTC

This package is auto-updated.

Last update: 2024-09-08 16:44:51 UTC


README

Laravel 5.1 Source License

轻松追踪数据库中模型的创建者/最后更新者。

设计成与Laravel迁移中的$table->timestamps()类似(并且可以并行使用),Attributions引入了$table->attributions()。这将向您的表中添加creator_id和updater_id列,以跟踪分别创建和更新模型的用户。默认情况下,它使用Laravel 5.1预定义的用户表,但可以自定义以引用任何表和键组合。

快速安装

您可以通过composer最轻松地安装此包

Laravel 5.1.x

composer require arkitecht/laravel-attributions

Schema Blueprint和Facades

此操作完成后,您可以更新Schema Facade以指向我们的嵌入式替换方案,该方案使用我们的Blueprint扩展类添加归属。

Laravel 5.1.x

Facade(在config/app.php中)

'Schema' => Illuminate\Support\Facades\Schema::class,

'Schema' => Arkitecht\Attributions\Facades\Schema::class,

您还可以手动使用归属构建器,而不像这样覆盖Facade

use Arkitecht\Attributions\Database\Schema\Blueprint;

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    $schema =  DB::getSchemaBuilder();

    $schema->blueprintResolver(function($table, $callback) {
        return new Blueprint($table, $callback);
    });

    $schema->create('tests', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->attributions();
    });
}

在迁移中使用它

要使迁移添加归属列,只需调用Blueprint::attributions方法。

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->attributions();
        });
    }
	
	...

}

您也可以使其引用替代表(来自users)或键(来自id)。

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->attributions('employees','employee_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('tests');
    }
}

在模型中使用它

要使创建者和更新者在模型创建和更新时自动更新,只需在模型中使用Attributions特质。

<?php

namespace App;

use Arkitecht\Attributions\Traits\Attributions;
use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
    use Attributions;
}
?>