alecgarcia / laravel-uid
Create UIDs like the ones Stripe generates. These can be used on your models or on their own.
v1.0.0
2022-01-30 02:58 UTC
Requires (Dev)
- illuminate/support: ~7|~8
- orchestra/testbench: ^6.24
- phpunit/phpunit: ~9.0
README
This package creates UIDs like the ones Stripe uses for your models or on their own.
Installation
Via Composer
$ composer require alecgarcia/laravel-uid
Usage
To use with a model
1. Add uid column to table
With an existing Model
- Add a migration
php artisan make:migration --table users add-uid-to-users
- Open up the migration file you just created and add a
uid
field.- If you overrode the column name in either the config for the default or in your model, make sure you create the column in your table to match.
/** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->string('uid', 32)->after('id')->unique(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('uid'); }); }
With a new Model
- Open up the migration file for the model and add a
uid
field- If you overrode the column name in either the config for the default or in your model, make sure you create the column in your table to match.
/** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('uid', 32)->unique(); $table->timestamps(); }); }
2. Add the trait to your model
<?php namespace App\Models; use Alecgarcia\LaravelUid\Traits\Uid; use Illuminate\Database\Eloquent\Model; class User extends Model { use UID; }
To used statically
<?php use Alecgarcia\LaravelUid\Uid; $generatedUid = Uid::make(); // Generated Uid -> "1mVQuIwVrRS9ijwx" // Defaults to no prefix and 16 Characters long $generatedUid = Uid::make('uid', 12); // Generated Uid -> "uid_nuv8V6GH" // Can set the prefix that is used and the length
Customization
Using the trait
You can add the following properties to your model to configure the Uid trait.
- This will override the defaults as well as the config file.
<?php namespace App\Models; use Alecgarcia\LaravelUid\Traits\Uid; use Illuminate\Database\Eloquent\Model; class User extends Model { use UID; public static string $uidPrefix = 'usr'; // Defaults to models class name public static int $uidLength = 10; // Defaults to 16 total characters public static bool $uidCheck = false; // Default is true public static string $uidColumn = 'uidCustomColumn'; // Default is uid }
Using the config file for defaults
- Publish the config file.
$ php artisan vendor:publish --tag laravel-uid.config
- Customize the defaults in the
app/config/laravel-uid.php
file.
<?php return [ /* |-------------------------------------------------------------------------- | Customize how the uid is created and used |-------------------------------------------------------------------------- */ 'characters' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'prefix_separator' => '_', 'uid_column' => 'uid', 'length' => 16, 'check' => true, ];
Change log
Please see the changelog for more information on what has changed recently.
Testing
$ php vendor/bin/phpunit
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email hello@alecgarcia.com instead of using the issue tracker.
Credits
- Alec Garcia
- All Contributors
- Originally influenced by dpods/laravel-uid
License
MIT. Please see the license file for more information.