laravel-uuid
Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.
Quick Overview
Laravel-uuid is a Laravel package that provides UUID (Universally Unique Identifier) functionality for Laravel applications. It allows easy generation and usage of UUIDs in Laravel models and database migrations.
Pros
- Seamless integration with Laravel's Eloquent ORM
- Supports both UUID v1 and v4 generation
- Provides a trait for easy implementation in models
- Compatible with multiple Laravel versions
Cons
- Limited to Laravel framework, not usable in other PHP projects
- Requires additional configuration for database columns
- May have performance implications when used extensively
- Not actively maintained (last update was in 2019)
Code Examples
- Generate a UUID:
use Webpatser\Uuid\Uuid;
$uuid = Uuid::generate();
echo $uuid; // Outputs: 123e4567-e89b-12d3-a456-426614174000
- Use UUID in a model:
use Webpatser\Uuid\Uuid;
class User extends Model
{
use HasUuid;
protected $primaryKey = 'uuid';
public $incrementing = false;
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{$model->getKeyName()} = Uuid::generate()->string;
});
}
}
- Create a migration with UUID:
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
Getting Started
-
Install the package via Composer:
composer require webpatser/laravel-uuid -
Add the service provider to
config/app.php:'providers' => [ // ... Webpatser\Uuid\UuidServiceProvider::class, ] -
Add the Facade to
config/app.php:'aliases' => [ // ... 'Uuid' => Webpatser\Uuid\Uuid::class, ] -
Use the
HasUuidtrait in your models and configure your migrations to use UUID fields as shown in the code examples above.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Laravel UUID Integration
Laravel package for generating and working with UUIDs. Automatic UUID model keys, validation rules, and Eloquent support.
Installation
composer require webpatser/laravel-uuid
Requirements: PHP 8.2+, Laravel 11.x or 12.x
Quick Start
use Illuminate\Support\Str;
use Webpatser\LaravelUuid\HasUuids;
// High-performance UUID generation
$uuid = Str::fastUuid(); // 15% faster than Str::uuid()
$ordered = Str::fastOrderedUuid(); // Database-optimized
// Eloquent model integration
class User extends Model
{
use HasUuids; // Automatic UUID generation
}
Documentation
For complete documentation, examples, and API reference, visit:
https://documentation.downsized.nl/laravel-uuid
License
MIT License.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot