waska14/laravel-uuid is a Laravel package for use uuid in models easily.
It currently has 5 GitHub stars and 16.105 downloads on Packagist (latest version 2.0.2).
Install it with composer require waska14/laravel-uuid.
Discover more Laravel packages by waska14
or browse all Laravel packages to compare alternatives.
Last updated
If you want to use uuid columns and fill them automatically, this package is for you.
This is a Laravel package for using (auto-generating) uuid in models. With this package you write less code, as the uuid(s) are being created, when you first save your instances.
Laravel | Compatible :---------|:---------- >=5.1 | ✓
Add the package in your composer.json by executing the command.
composer require waska14/laravel-uuid
Next, add the service provider in your config/app.php
'providers' => [
....
Waska\LaravelUuid\UuidServiceProvider::class,
....
],
// 2.1 If you want to use uuid column as primary column:
Schema::create('students', function(Blueprint $table)
{
$table->uuid('uuid')->primary();
$table->string('name');
$table->string('last_name');
...
});
// 2.2 If you want to use uuid column as non-primary column:
Schema::create('students', function(Blueprint $table)
{
$table->increments('id');
$table->uuid('uuid')->unique();
$table->string('name');
$table->string('last_name');
...
});
// 2.3 If you want to use multiple uuid columns (maybe on of them is primary):
Schema::create('students', function(Blueprint $table)
{
$table->uuid('uuid')->primary();
$table->uuid('uuid_column_2')->unique();
$table->uuid('uuid_column_3')->unique();
$table->string('name');
$table->string('last_name');
...
});
Note: If uuid column is not primary, you should make it unique, or manually index the column.
class Student extends Model
{
use \Waska\Traits\Uuid;
}
2.1 If you are using uuid column as primary key and column_name is default id, you need nothing more.
2.2 If you are using uuid column as primary key and column_name is not default id, you must
define protected $primaryKey and protected $uuid_column:
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $primaryKey = "uuid_primary_column_name";
protected $uuid_column = "uuid_primary_column_name";
protected $fillable = [
'name',
'last_name',
];
}
2.3 If you are using non-primary uuid column and column name equals to default_column_name (uuid from config),
you need only to append column name in protected $fillable:
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $fillable = [
'name',
'last_name',
'uuid', // config/waska.uuid.php -> default_column_name
];
}
2.4 If you are using non-primary uuid column and column name doesn't equal to default_column_name (uuid from config),
you need define protected $uuid_column and append column name in protected $fillable:
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $uuid_column = "uuid_column_name";
protected $fillable = [
'name',
'last_name',
'uuid_column_name',
];
}
2.5 If you are using multiple uuid columns (if one of them is primary, you must do step 2.1 at first),
you need define protected $uuid_column as an array and append column names (only non-primary) in protected $fillable
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $primaryKey = "uuid_primary_column_name"; // If one of them is primary
protected $uuid_column = ["uuid_column_name1", "uuid_column_name2", "uuid_column_name3"];
protected $fillable = [
'name',
'last_name',
'uuid_column_name1',
'uuid_column_name2',
'uuid_column_name3',
];
}
2.6 Now when you create new Student object, uuid(s) will be generated automatically
Student::create([
'name' => "Bill',
'last_name' => "Gates',
]);
// or
$student = new Student([
'name' => "Bill',
'last_name' => "Gates',
]);
$student->save();
// or
$student = new Student();
$student->name = "Bill";
$student->last_name = "Gates";
$student->save();
If you want to change default configuration, you must publish default configuration file to your project.
You can publish config by running this in console:
php artisan vendor:publish --tag=waska-uuid-config
Note: If you're going to use v3 or v5 uuid, it's recommended to change v3_default_namespace and v5_default_namespace with valid uuid strings .
Generate them with this command in tinker (Start tinker: php artisan tinker)
Waska\Uuid::get(4); // This command will generate valid uuid (v4, pseudo-random) string
Generate uuid (Universal Unique Identifier)
/**
* This generates name-based Uuid
* @param int $version.
* @param string $name. String which the uuid is generating for.
* @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
* @return String
*/
Waska\Uuid::get(3, "some_random_string", "valid_uuid");
// This generates pseudo-random Uuid
Waska\Uuid::get(); // Default version is 4, so it means: Waska\Uuid::get(4);
/**
* This generates name-based Uuid
* @param int $version.
* @param string $name. String which the uuid is generating for.
* @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
* @return String
*/
Waska\Uuid::get(5, "some_random_string", "valid_uuid");