rawaby88/muid

为您的Eloquent模型生成带有自定义前缀的随机ID。

4.0.2 2024-05-07 20:40 UTC

This package is auto-updated.

Last update: 2024-09-07 21:16:06 UTC


README

Latest Version on Packagist GitHub Actions GitHub license

Laravel包,用于为Eloquent模型生成带有前缀的随机ID

###示例

Eloquent MUID

Muid有3种长度可供选择

如果需要,您可以从配置文件中更改长度。

安装

您可以通过Composer安装此包

composer require rawaby88/muid

用法

您可以通过扩展提供的模型类或使用特性来实现

扩展模型

创建Eloquent模型时,不要扩展标准的Laravel模型类,而是扩展此包提供的模型类

namespace App\Models;

use Rawaby88\Muid\Database\Eloquent\Model;

class Organization extends Model
{
    /**
	 * The "prefix" of the MUID.
	 *
	 * @var string
	 */
	protected $keyPrefix = 'org_';
}

扩展用户模型

扩展此包提供的User类

<?php

namespace App\Models;

use \Rawaby88\Muid\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
	 * The "prefix" of the MUID.
	 *
	 * @var string
	 */
	protected $keyPrefix = 'user_';
}

使用特性

作为上述示例中扩展类的替代方案,您还可以使用提供的特性

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Rawaby88\Muid\Database\Eloquent\Muid;

class Organization extends Model
{
    use Muid;
    
    /**
	 * The "prefix" of the MUID.
	 *
	 * @var string
	 */
	protected $keyPrefix = 'organization_';
}

前缀

为了生成muid的前缀,您需要在模型中提供此信息,通过添加$keyPrefix,如果没有提供前缀,muid将不带前缀生成

/**
 * The "prefix" of the MUID.
 *
 * @var string
 */
protected $keyPrefix = 'example_';

创建模型

除了make:model artisan命令外,您现在还可以使用muid:make:model,该命令具有标准make:model命令的所有功能(但无法创建pivot模型)

php artisan muid:make:model Models/Organization --all

数据库迁移

此包包含所有类型,可轻松生成MUID

可用的蓝图列表

迁移示例

<?php

Schema::create( 'model_with_primaryMuid_test', function ( Blueprint $table ): void
{
    $table->primaryMuid( 'id' );
    $table->string( 'name' );
    $table->timestamps();
} );

Schema::create( 'model_with_muid_test', function ( Blueprint $table ): void
{
    $table->muid( 'id' )->primary();
    $table->string( 'name' );
    $table->timestamps();
} );

Schema::create( 'model_with_foreignMuid_test', function ( Blueprint $table ): void
{
    $table->muid( 'id' )->primary();
    $table->foreignMuid( 'model_with_muid_test_id' )->constrained( 'model_with_muid_test' );
    $table->timestamps();
} );

Schema::create( 'model_with_muidMorph_test', function ( Blueprint $table ): void
{
    $table->muid( 'id' )->primary();
    $table->muidMorphs( 'testable' );
    $table->timestamps();
} );

Schema::create( 'model_with_nullableMuidMorphs_test', function ( Blueprint $table ): void
{
    $table->muid( 'id' )->primary();
    $table->nullableMuidMorphs( 'testable' );
    $table->timestamps();
} );

Schema::create( 'model_without_muid_test', function ( Blueprint $table ): void
{
    $table->primaryMuid( 'id' );
    $table->timestamps();
} );

发布配置

完成后,使用以下命令将配置发布到您的配置文件夹

php artisan vendor:publish --provider="Rawaby88\Muid\MuidServiceProvider"

配置

/*
|--------------------------------------------------------------------------
| Muid length
|--------------------------------------------------------------------------
|
| Here you can change the MUID length.
| remember that length include [prefix, timestamp(6)chars] and the rest will be random bits
| recommended to have minimum of 16 chars
|
*/
'tiny_muid_length'  => 16,
'small_muid_length' => 24,
'muid_length'       => 36,

/*
|--------------------------------------------------------------------------
| Random string strings
|--------------------------------------------------------------------------
|
| Recommended not to change
|
*/
'alfa_small'   => 'abcdefghilkmnopqrstuvwxyz',
'alfa_capital' => 'ABCDEFGHILKMNOPQRSTUVWXYZ',
'digits'       => '0123456789',

/*
|--------------------------------------------------------------------------
| Capital Char options
|--------------------------------------------------------------------------
|
| Set it to FALSE if you wish not to use capital letters in the generated MUID
|
*/
'allow_capital' => TRUE,

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献

安全

如果您发现任何安全相关的问题,请通过电子邮件github@dreamod.pl而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件

Laravel包模板

此包是用Laravel包模板生成的。