dereffi/issuing

为 Laravel 5.* 构建的 PHP 包,用于在数据库表中保存创建者、最后更新者和删除者

1.0.0 2019-06-20 10:22 UTC

This package is auto-updated.

Last update: 2024-09-20 21:57:47 UTC


README

DerEffi/laravel-issuing 是一个为 Laravel 5.* 构建的 PHP 包,用于在数据库表中保存创建者、最后更新者和删除者

在 Packagist 上查看

版本

此包符合 Laravel 5.8 规范。 当前包版本 0.1.0

关于

  • 轻松在迁移文件中添加 created_byupdated_bydeleted_by
  • 在 eloquent 事件上自动存储数据到指定的列
  • 自动在选定的模型上添加关系
  • 通过配置文件轻松自定义

安装

此项目可以通过 Composer 安装。要获取 Issuing 包的最新版本,请运行以下命令

composer require dereffi/issuing

安装后,您可以通过以下命令发布配置文件

php artisan vendor:publish --tag=dereffi-issuing

用法

配置

表示 Issuer 的模型必须实现 Illuminate\Contracts\Auth\Authenticatable 接口 (或其任何子类),这是 Eloquent User 模型的默认设置。

在配置文件 issuing.php 中轻松配置 Issuer 模型和数据库列

迁移

要自动在迁移表中创建 *_by 列,您需要在 Schema::create 函数中的任何位置调用 IssuerColumns 类,并将 $table 作为参数。列名可以在 config/issuing.php 文件中更改。如果您这样做,您必须刷新迁移或在迁移表之前执行此操作。

以下示例使用默认的 Laravel 用户迁移文件

<?php

use Dereffi\Issuing\IssuerColumns;              // <-- Import

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

class CreateUsersTable extends Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

            IssuerColumns::create($table);      // <-- auto adding the columns

        });
    }

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

模型

要自动在创建、更新或删除元素时保存发行者 ID,您必须在模型文件中使用 Dereffi\Issuing\Issuable 特性,如下例所示的默认 Laravel 用户模型

<?php

namespace App;

use Dereffi\Issuing\Issuable;               // <-- add this line to Import the Trait

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, Issuable;               // <-- add the Issuable Trait to the Model

许可证

Laravel 用户验证受 MIT 许可证 (MIT) 许可。