sysbox/laravel-base-entity

非数值ID和常见属性(id、active、created、created_at、updated、updated_at)的基础模型

v1.0.14 2020-05-15 03:04 UTC

This package is auto-updated.

Last update: 2024-09-15 12:45:31 UTC


README

此包为Laravel模型提供6个关键字段的基础模型

  • id(非数值/非递增)
  • active(布尔值)
  • created_by_id(创建者的ID)
  • created_at(创建时间戳)
  • updated_by_id(更新者的ID)
  • updated_at(更新时间戳)

安装

通过Composer

$ composer require sysbox/laravel-base-entity

设置

###步骤1:发布配置文件

$ php artisan vendor:publish --provider='Sysbox\LaravelBaseEntity\LaravelBaseEntityServiceProvider'

###步骤2:设置您的Laravel接口模型,如下所示:编辑 <laravel-root-dir>/app/User.php

class User extend BaseModel implements UserReferable <<<<<<<<< implement this interface
{

    /**
     * @return mixed <<<<<<<<< add this function
     */
    public function getUserId()
    {
        return $this->id;
    }

    /**
     * @return \Illuminate\Contracts\Auth\Authenticatable|UserReferable|null
     */
    public static function getCurrentUser() <<<<<<<<< add this function
    {
        return Auth::user();
    }
}

###步骤3:设置配置文件:编辑 <laravel-root-dir>/config/LaravelBaseEntity.php,您将看到类似的内容

return [


    // this is the user class that will be refer to for fields: created_by_id and updated_by_id
    'user_class' => User::class, <<<<<<<<< add user class here.

    // this is the system user's id,
    // leave it BLANK for auto generation.
    // or specifiy in env, ie: 'system_user_id' => env('SYSTEM_USER_ID'),
    'system_user_id' => '',
];

使用方法

现在您应该能够创建任何Laravel模型,只需扩展BaseModel即可,如下所示

use Sysbox\LaravelBaseEntity\BaseModel;
class Person extend BaseModel <<<<<<<<< extend your BaseModel
{
    // all other laravel function for the model..
}

您还可以在迁移中创建列,如下所示

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            LaravelBaseEntity::addBasicLaravelBaseEntityColumns($table);
            $table->string('name');
            //or add column
            // LaravelBaseEntity::addHashIdColumn($table, 'another_user_id');
        });
    }

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