This package creates UIDs like the ones Stripe uses for your models or on their own.
$ composer require alecgarcia/laravel-uid
- 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');
});
}
- 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();
});
}
<?php
namespace App\Models;
use Alecgarcia\LaravelUid\Traits\Uid;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use UID;
}
<?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
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
}
- 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,
];
Please see the changelog for more information on what has changed recently.
$ php vendor/bin/phpunit
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
- Alec Garcia
- All Contributors
- Originally influenced by dpods/laravel-uid
MIT. Please see the license file for more information.