skore-labs/laravel-auditable

审计执行您应用程序模型操作的用户

2.6.0 2024-05-24 11:40 UTC

This package is auto-updated.

Last update: 2024-09-24 12:22:13 UTC


README

审计执行您应用程序模型操作的用户

状态

packagist version tests StyleCI Codacy Badge Codacy Badge Scc Count Badge Scc Count Badge

入门指南

composer require skore-labs/laravel-auditable

然后在您希望具有可审计功能的模型上写入以下内容

<?php

namespace SkoreLabs\LaravelAuditable\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use SkoreLabs\LaravelAuditable\Traits\Auditable;

class Post extends Model
{
    use Auditable;
    // Add this one and it will auto-detect for the deletedBy
    // use SoftDeletes;

    /**
     * The attributes that should be visible in serialization.
     *
     * @var array
     */
    protected $visible = ['title', 'content'];
}

在您的迁移文件中应该看起来像这样

<?php

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

class CreatePostsTestTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('content');

            $table->timestamps();
            $table->softDeletes();
            $table->auditables();

            // or if has softDeletes (deleted_at column):
            $table->auditables(true);

            // also you might want to rename the users table:
            $table->auditables(true, 'my_users_table');
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

注意:如果您想在迁移的down方法中删除它,您可以使用dropAuditables

    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropAuditables();
            // or if has softDeletes (deleted_at column):
            $table->dropAuditables(true);
        });
    }

方法

这些都是Auditable特质为您的模型提供的关系

$post = Post::first();

$post->createdBy; // Author of the post
$post->updatedBy; // Author of the last update of the post
$post->deletedBy; // Author of the deletion of the post

$post->author; // Alias for createdBy

支持

这个和我们的所有Laravel包都尽可能地遵循Laravel的LTS支持。

阅读更多: https://laravel.net.cn/docs/master/releases#support-policy

致谢