├── .gitignore ├── README.md ├── composer.json ├── composer.lock ├── config └── muid.php ├── database └── migrations │ ├── 2023_08_14_132400_create_table_does_not_have_muid_as_primary_key_test_table.php │ └── 2023_08_14_132400_create_table_test_table.php ├── example.php ├── phpunit.xml ├── src ├── HasMUID.php ├── MUIDHelper.php ├── Models │ ├── TableDoesNotHaveMUIDAsPrimaryKeyTest.php │ └── TableTest.php └── Providers │ ├── BaseServiceProvider.php │ ├── BlueprintMacroServiceProvider.php │ ├── ConfigurationServiceProvider.php │ └── StrSupportMacroServiceProvider.php └── tests ├── Feature ├── CreateTableTest.php ├── InitialTest.php └── generatingUniqueMUIDTest.php └── TestCase.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.phpunit.cache 2 | /node_modules 3 | /public/build 4 | /public/hot 5 | /public/storage 6 | /storage/*.key 7 | /vendor 8 | .env 9 | .env.backup 10 | .env.production 11 | .phpunit.result.cache 12 | Homestead.json 13 | Homestead.yaml 14 | auth.json 15 | npm-debug.log 16 | yarn-error.log 17 | /.fleet 18 | /.idea 19 | /.vscode 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel UUID/MUID 2 | 3 | This package is for generating uuid but with custom length and charset. 4 | 5 | ## Setup 6 | ### Instaltion 7 | 8 | Use the package manager [composer](https://getcomposer.org/) to install muid. 9 | 10 | ```bash 11 | composer require abdulrhmansouda/muid 12 | ``` 13 | 14 | ### Configuration 15 | if you want to change the defalut value you can publish configurations files by run: 16 | ```bash 17 | php artisan vendor:publish --tag=muid-config 18 | ``` 19 | 20 | ## Usage 21 | 22 | inside your table migration 23 | ```php 24 | public function up(): void 25 | { 26 | Schema::create('table_name', function (Blueprint $table) { 27 | $table->muid()->primary(); \\ by default the column name is "muid" 28 | $table->timestamps(); 29 | }); 30 | } 31 | ``` 32 | 33 | inside your model 34 | ```php 35 | use Illuminate\Database\Eloquent\Model; 36 | use MUID\HasMUID; 37 | 38 | class TableName extends Model 39 | { 40 | use HasMUID; 41 | } 42 | ``` 43 | 44 | That's it ^_^ 45 | 46 | ## Customization 47 | 48 | you are able to add multiple column, change column_name, length of the string inside column, the charset. 49 | 50 | first of all to change column name you have to start from migration: 51 | 52 | ```php 53 | public function up(): void 54 | { 55 | Schema::create('table_name', function (Blueprint $table) { 56 | $table->muid('id')->primary(); 57 | $table->muid('unique_code', 5)->unique(); 58 | $table->timestamps(); 59 | }); 60 | } 61 | ``` 62 | then from your model you can change the column_name, length , charset as follow: 63 | 64 | ```php 65 | use Illuminate\Database\Eloquent\Model; 66 | use MUID\HasMUID; 67 | 68 | class TableName extends Model 69 | { 70 | use HasMUID; 71 | 72 | protected static function get_muid_columns(): array 73 | { 74 | return [ 75 | [ 76 | 'column_name' => 'id', 77 | // 'length' => 10, default length is 10 78 | // 'charset' => '0123456789abcdefghijklmnopqrstuvwxyz', default chareset 79 | ], 80 | [ 81 | 'column_name' => 'unique_code', 82 | 'length' => 5, 83 | 'charset' => '0123456789', 84 | ], 85 | ]; 86 | } 87 | } 88 | ``` 89 | ## Helper Function 90 | ### when you want to generate muid manually. 91 | 92 | Depending on the model the parameters of length and charset will be taken automatically. 93 | ```php 94 | use Illuminate\Support\Str; 95 | 96 | $unique_muid = Str::generateMUIDByModel(ModelName::class); // default column name is muid. 97 | 98 | $unique_muid = Str::generateMUIDByModel(ModelName::class, 'column_name'); 99 | ``` 100 | 101 | When you don't want to use model at all. you can generate unique muid depending on the table name. 102 | 103 | ```php 104 | use Illuminate\Support\Str; 105 | 106 | $unique_muid = Str::generateMUIDByTable('table_name'); 107 | // default column_name = 'muid' 108 | // default column_length = 10 109 | // default charset = '0123456789abcdefghijklmnopqrstuvwxyz' 110 | 111 | $unique_muid = Str::generateMUIDByTable('table_name', 'column_name', 5, '0123456789'); 112 | ``` 113 | 114 | ## To add muid column to a table which has records 115 | after adding configuration to model you have to add in migration. 116 | 117 | ```php 118 | use Illuminate\Database\Migrations\Migration; 119 | use Illuminate\Database\Schema\Blueprint; 120 | use Illuminate\Support\Facades\Schema; 121 | 122 | return new class extends Migration 123 | { 124 | /** 125 | * Run the migrations. 126 | */ 127 | public function up(): void 128 | { 129 | Schema::table('table_name', function (Blueprint $table) { 130 | $table->muid('new_column_name')->nullable(); 131 | }); 132 | 133 | TableName::all()->each(function ($model_instance) { 134 | $model_instance->generateMUID(['new_column_name']); 135 | $model_instance->save(); 136 | }); 137 | 138 | Schema::table('table_name', function (Blueprint $table) { 139 | $table->muid('new_column_name') 140 | ->nullable(false) 141 | ->change(); 142 | }); 143 | } 144 | 145 | /** 146 | * Reverse the migrations. 147 | */ 148 | public function down(): void 149 | { 150 | Schema::table('table_name', function (Blueprint $table) { 151 | $table->dropColumn(['new_column_name']); 152 | }); 153 | } 154 | }; 155 | ``` 156 | 157 | ## Contributing 158 | 159 | Pull requests are welcome. For major changes, please open an issue first 160 | to discuss what you would like to change. 161 | 162 | Please make sure to update tests as appropriate. 163 | 164 | ## License 165 | 166 | [MIT](https://choosealicense.com/licenses/mit/) -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "abdulrhmansouda/muid", 3 | "description": "This package for unique id with dynamic options characters", 4 | "type": "library", 5 | "require": { 6 | "php": ">=5.3.0", 7 | "illuminate/database": "*", 8 | "illuminate/support": "*" 9 | }, 10 | "license": "proprietary", 11 | "authors": [ 12 | { 13 | "name": "Abdul Rahman Souda", 14 | "email": "eng.abdul.rahman19992@gmail.com" 15 | } 16 | ], 17 | "minimum-stability": "dev", 18 | "autoload": { 19 | "psr-4": { 20 | "MUID\\": "src/" 21 | } 22 | }, 23 | "autoload-dev": { 24 | "psr-4": { 25 | "MUID\\Tests": "tests/" 26 | } 27 | }, 28 | "extra": { 29 | "laravel": { 30 | "providers": [ 31 | "MUID\\Providers\\ConfigurationServiceProvider", 32 | "MUID\\Providers\\BlueprintMacroServiceProvider", 33 | "MUID\\Providers\\StrSupportMacroServiceProvider" 34 | ] 35 | } 36 | }, 37 | "require-dev": { 38 | "orchestra/testbench": "dev-develop" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /config/muid.php: -------------------------------------------------------------------------------- 1 | [ 4 | 'charset' => '0123456789abcdefghijklmnopqrstuvwxyz', 5 | 'column_length' => 10, 6 | ], 7 | ]; 8 | -------------------------------------------------------------------------------- /database/migrations/2023_08_14_132400_create_table_does_not_have_muid_as_primary_key_test_table.php: -------------------------------------------------------------------------------- 1 | id(); 16 | // $table->muid()->primary(); 17 | $table->muid('unique_code',5); 18 | $table->timestamps(); 19 | }); 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | */ 25 | public function down(): void 26 | { 27 | Schema::dropIfExists('table_test'); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /database/migrations/2023_08_14_132400_create_table_test_table.php: -------------------------------------------------------------------------------- 1 | id(); 16 | $table->muid()->primary(); 17 | $table->muid('unique_code',5); 18 | $table->timestamps(); 19 | }); 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | */ 25 | public function down(): void 26 | { 27 | Schema::dropIfExists('table_test'); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | tests/Unit 10 | 11 | 12 | tests/Feature 13 | 14 | 15 | 16 | 17 | 18 | src 19 | 20 | 21 | 32 | 40 | 41 | -------------------------------------------------------------------------------- /src/HasMUID.php: -------------------------------------------------------------------------------- 1 | 'muid', 12 | 'length' => config('muid.default.column_length'), 13 | 'charset' => config('muid.default.charset'), 14 | ] 15 | ]; 16 | } 17 | 18 | protected static function bootHasMUID() 19 | { 20 | foreach (static::get_muid_columns() as $column) { 21 | static::creating(function ($record) use ($column) { 22 | $column_name = $column['column_name']; 23 | $muid_length = (isset($column['length']) && is_numeric($column['length'])) ? $column['length'] : config('muid.default.column_length'); 24 | $muid_charset = (isset($column['charset']) && is_string($column['charset'])) ? $column['charset'] : config('muid.default.charset'); 25 | do { 26 | $unique_code = MUIDHelper::generateRandomString($muid_length, $muid_charset); 27 | } while (static::where($column_name, $unique_code)->exists()); 28 | $record->{$column_name} = $unique_code; 29 | }); 30 | } 31 | } 32 | 33 | public function generateMUID($column_names = ['muid']) 34 | { 35 | $collections = collect(self::get_muid_columns()); 36 | foreach ($column_names as $column_name) { 37 | $column = $collections->where('column_name', $column_name)->first(); 38 | $muid_length = (isset($column['length']) && is_numeric($column['length'])) ? $column['length'] : config('muid.default.column_length'); 39 | $muid_charset = (isset($column['charset']) && is_string($column['charset'])) ? $column['charset'] : config('muid.default.charset'); 40 | do { 41 | $unique_code = MUIDHelper::generateRandomString($muid_length, $muid_charset); 42 | } while (static::where($column_name, $unique_code)->exists()); 43 | $this->{$column_name} = $unique_code; 44 | } 45 | } 46 | 47 | /** 48 | * Get the columns that should receive a unique identifier. 49 | * 50 | * @return array 51 | */ 52 | public function uniqueIds() 53 | { 54 | return collect(self::get_muid_columns())->pluck('column_name')->toArray(); 55 | } 56 | 57 | /** 58 | * Get the auto-incrementing key type. 59 | * 60 | * @return string 61 | */ 62 | public function getKeyType() 63 | { 64 | if (in_array($this->getKeyName(), $this->uniqueIds())) { 65 | return 'string'; 66 | } 67 | 68 | return $this->keyType; 69 | } 70 | 71 | /** 72 | * Get the value indicating whether the IDs are incrementing. 73 | * 74 | * @return bool 75 | */ 76 | public function getIncrementing() 77 | { 78 | if (in_array($this->getKeyName(), $this->uniqueIds())) { 79 | return false; 80 | } 81 | 82 | return $this->incrementing; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/MUIDHelper.php: -------------------------------------------------------------------------------- 1 | where('column_name', $column_name)->first(); 25 | $muid_length = (isset($column['length']) && is_numeric($column['length'])) ? $column['length'] : config('muid.default.column_length'); 26 | $muid_charset = (isset($column['charset']) && is_string($column['charset'])) ? $column['charset'] : config('muid.default.charset'); 27 | do { 28 | $unique_code = MUIDHelper::generateRandomString($muid_length, $muid_charset); 29 | } while ($model_class_name::where($column_name, $unique_code)->exists()); 30 | return $unique_code; 31 | } 32 | 33 | public static function generateMUIDByTable($table_name, $column_name = 'muid', $column_length = null, $charset = null): string 34 | { 35 | $column_length = (is_null($column_length)) ? config('muid.default.column_length') : $column_length; 36 | $charset = (is_null($charset)) ? config('muid.default.charset') : $charset; 37 | 38 | do { 39 | $unique_code = MUIDHelper::generateRandomString($column_length, $charset); 40 | } while (DB::table($table_name)->where($column_name, $unique_code)->exists()); 41 | 42 | return $unique_code; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Models/TableDoesNotHaveMUIDAsPrimaryKeyTest.php: -------------------------------------------------------------------------------- 1 | 'muid', 19 | // ], 20 | [ 21 | 'column_name' => 'unique_code', 22 | 'length' => 5, 23 | 'charset' => '0123456789', 24 | ], 25 | ]; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Models/TableTest.php: -------------------------------------------------------------------------------- 1 | 'muid', 19 | ], 20 | [ 21 | 'column_name' => 'unique_code', 22 | 'length' => 5, 23 | 'charset' => '0123456789', 24 | ], 25 | ]; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Providers/BaseServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerResources(); 27 | } 28 | 29 | private function registerResources(){ 30 | $this->loadMigrationsFrom(__DIR__.'/../../database/migrations'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Providers/BlueprintMacroServiceProvider.php: -------------------------------------------------------------------------------- 1 | char($column_name, $column_length); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Providers/ConfigurationServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/../../config/muid.php', 'muid'); 17 | } 18 | 19 | /** 20 | * Bootstrap services. 21 | * 22 | * @return void 23 | */ 24 | public function boot() 25 | { 26 | $this->publishes([ 27 | __DIR__.'/../../config/muid.php' => config_path('muid.php'), 28 | ],'muid-config'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Providers/StrSupportMacroServiceProvider.php: -------------------------------------------------------------------------------- 1 | make('config'), function (Repository $config) { 35 | $config->set('database.default', 'mysql'); 36 | $config->set('database.connections.mysql', [ 37 | 'driver' => 'mysql', 38 | 'host' => '127.0.0.1', 39 | 'database' => 'testing_db', 40 | 'username' => 'root', 41 | 'password' => '', 42 | ]); 43 | }); 44 | } 45 | public function test_table_test() 46 | { 47 | $instance = TableTest::create(); 48 | 49 | $this->assertEquals(10, strlen($instance->muid)); 50 | $this->assertEquals(5, strlen($instance->unique_code)); 51 | } 52 | 53 | public function test_table_does_not_have_muid_as_primary_key_test() 54 | { 55 | $instance = TableDoesNotHaveMUIDAsPrimaryKeyTest::create(); 56 | 57 | $this->assertIsInt($instance->id); 58 | $this->assertEquals(5, strlen($instance->unique_code)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/Feature/InitialTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/Feature/generatingUniqueMUIDTest.php: -------------------------------------------------------------------------------- 1 | make('config'), function (Repository $config) { 38 | $config->set('database.default', 'mysql'); 39 | $config->set('database.connections.mysql', [ 40 | 'driver' => 'mysql', 41 | 'host' => '127.0.0.1', 42 | 'database' => 'testing_db', 43 | 'username' => 'root', 44 | 'password' => '', 45 | ]); 46 | }); 47 | } 48 | 49 | public function test_generating_muid_by_model() 50 | { 51 | $unique_muid = MUIDHelper::generateMUIDByModel(TableTest::class); 52 | $this->assertEquals(10, strlen($unique_muid)); 53 | 54 | $unique_muid = MUIDHelper::generateMUIDByModel(TableTest::class, 'unique_code'); 55 | $this->assertEquals(5, strlen($unique_muid)); 56 | } 57 | 58 | public function test_generating_muid_by_table() 59 | { 60 | $unique_muid = MUIDHelper::generateMUIDByTable('table_test'); 61 | $this->assertEquals(10, strlen($unique_muid)); 62 | 63 | $unique_muid = MUIDHelper::generateMUIDByTable('table_test', 'unique_code', 5); 64 | $this->assertEquals(5, strlen($unique_muid)); 65 | } 66 | 67 | public function test_generating_muid_using_Str_support() 68 | { 69 | $unique_muid = Str::generateMUIDByModel(TableTest::class); 70 | $this->assertEquals(10, strlen($unique_muid)); 71 | 72 | $unique_muid = Str::generateMUIDByModel(TableTest::class, 'unique_code'); 73 | $this->assertEquals(5, strlen($unique_muid)); 74 | 75 | $unique_muid = Str::generateMUIDByTable('table_test'); 76 | $this->assertEquals(10, strlen($unique_muid)); 77 | 78 | $unique_muid = Str::generateMUIDByTable('table_test', 'unique_code', 5); 79 | $this->assertEquals(5, strlen($unique_muid)); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | make('config'), function (Repository $config) { 30 | $config->set('database.default', 'mysql'); 31 | $config->set('database.connections.mysql', [ 32 | 'driver' => 'mysql', 33 | 'database' => 'dbtest', 34 | 'username' => 'root', 35 | 'password' => '', 36 | ]); 37 | }); 38 | } 39 | } 40 | --------------------------------------------------------------------------------