rollswan / uuid
Generates Universally Unique IDentifiers as primary key
1.0.0
2020-10-20 05:28 UTC
This package is auto-updated.
Last update: 2025-04-05 01:36:53 UTC
README
A Laravel package to generate uuid as primary keys.
How to use?
- Install
composer require rollswan/uuid
- use
uuid()
in migration,
Example:
Schema::create('posts', function (Blueprint $table) {
$table->uuid(); // you can also custom your uuid `uuid('post_uuid')`
$table->string('title');
$table->string('body');
$table->timestamps();
});
- use
WithUuid
trait in your Model and declare the primary key
Example:
namespace App;
use Rollswan\Uuid\Traits\WithUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use WithUuid;
protected $primaryKey = 'uuid';
}
Done! This will automatically generate unique UUID whenever you save a new record.