codiliateur/eloquent-model-userstamps

此软件包提供了一种Eloquent模型特性,可以自动维护您的模型列(created_by、updated_by、deleted_by)中的用户戳。

v1.0.3 2023-12-17 11:57 UTC

This package is not auto-updated.

Last update: 2024-09-22 15:51:31 UTC


README

安装

此软件包可以使用composer安装。

composer require codiliateur/eloquent-model-userstamps

模型特性

将特性 Codiliateur\Userstamps\Models\HasUserstamps 添加到您的模型中。

class YourModel extends Model
{
    use Codiliateur\Userstamps\Models\HasUserstamps;
    ...
}    

要自定义用户戳列的名称,在您的特定模型中定义常量 CREATED_BYUPDATED_BYDELETED_BY

class YourModel extends Model
{
    use Codiliateur\Userstamps\Models\HasUserstamps;
    ...
    
    const CREATED_BY = 'creator_id';
    const UPDATED_BY = 'updater_id';
    const DELETED_BY = 'liquidator_id';
    
    ...
}    

迁移助手

此软件包自动向 Blueprint 添加两个助手方法 userstamps()dropUserstamps()

    // to add user stamp columns
    userstamps(bool $softDeletes = false, string $columnType = null, array $columnNames = null)

    // to drop user stamp columns
    dropUserstamps(bool $softDeletes = false, array $columnNames = null)

要将用户戳列添加到您的表中,请在迁移的 up() 中添加行。

对于没有 SoftDeleting 特性的模型

$table->userstamps();

或第一个参数相同

$table->userstamps(false);  // the same

对于使用 SoftDeleting 特性的模型

$table->userstamps(true);

默认情况下,添加的列具有 bigInteger 类型。

更改列类型,您可以将 smallIntegerintegerbigIntegeruuid 放入第二个参数 $columnType。例如

$table->userstamps(false, 'uuid');

自定义列名称,您可以将名称数组放入第三个参数 $columnNames

    $table->userstamps(false, null, [
        'creator_id',    // [0] instead of 'created_by'
        'updater_id',    // [1] instead of 'updated_by'
        'liquidator_id', // [2] instead of 'deleted_by'
    ]);

不要忘记在 dropUserstams() 调用中添加任何参数。

配置软件包

默认情况下,用于用户戳列的名称为 created_byupdated_bydeleted_by,这些列的类型为 bigInteger。这些列名称和列类型在软件包配置文件中定义。

您可以将此配置文件发布到您的项目中。为此,请运行以下命令

php artisan vendor:publish --provider="Codiliateur\Userstamps\UserstampsServiceProvider"

文件 .\config\codiliateur\userstamps.php 将被发布。

要更改 用户戳列的类型,请使用配置选项 column_type

例如

'column_type' => 'uuid', // if user ID has 'uuid' type

在此处,您可以使用 smallIntegerintegerbigIntegeruuid 值。

要更改默认的 用户戳列的名称,您必须修改配置选项 columns

例如

    'columns' => [
        'creator_id',    // [0] instead of 'created_by'
        'updater_id',    // [1] instead of 'updated_by'
        'liquidator_id', // [2] instead of 'deleted_by'
    ],