rawaby88 / muid
Generate a random id with your own prefix for your Eloquent models.
Requires
- php: ^8.0 || ^8.1 || ^8.2
- doctrine/dbal: ^4
Requires (Dev)
- ext-pdo: *
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10
README
Laravel package to generate a random ID with your prefix for your Eloquent models
###Example
Available 3 lengths of Muid
the length can be altered from the config file if you wish.
Installation
You can install the package via Composer:
composer require rawaby88/muid
Usage
You can extend the provided model classes, or by using a trait
Extending model
When creating an Eloquent model, instead of extending the standard Laravel model class, extend from the model class provided by this package:
namespace App\Models; use Rawaby88\Muid\Database\Eloquent\Model; class Organization extends Model { /** * The "prefix" of the MUID. * * @var string */ protected $keyPrefix = 'org_'; }
Extending user model
Extending the User class provided by this package:
<?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_'; }
Using trait
As an alternative to extending the classes in the examples above, you also have the ability to use the provided trait instead
<?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_'; }
prefix
in order to generate the prefix for your muid, you will need to provide this information in the model itself
by adding $keyPrefix
, if no prefix provided muid will be generated without prefix
/** * The "prefix" of the MUID. * * @var string */ protected $keyPrefix = 'example_';
Creating models
In addition to the make:model
artisan command, you will now have access to
muid:make:model
which has all the functionality of the standard make:model
command (with the exception of not being able to create a pivot model):
php artisan muid:make:model Models/Organization --all
Database migration
This package includes all types to generate your MUID in an easy way
lists of available Blueprints:
Migration example
<?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(); } );
Publish Config
Once done, publish the config to your config folder using:
php artisan vendor:publish --provider="Rawaby88\Muid\MuidServiceProvider"
Configuration
/* |-------------------------------------------------------------------------- | 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,
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email github@dreamod.pl instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.