broadway/uuid-generator
UUID generator utilities for your application, powered by ramsey/uuid. Includes a standard generator plus testing helpers for predictable UUIDs in tests. Comes with a runnable example script in the examples directory.
Installation: Add the package via Composer in your Laravel project:
composer require broadway/uuid-generator
Ensure your composer.json includes PHP 7.2+ and ramsey/uuid (auto-installed as a dependency).
First Use Case: Generate a UUID in a Laravel model or service:
use Broadway\UuidGenerator\UuidGenerator;
$uuid = UuidGenerator::generate(); // Returns a Ramsey\Uuid\UuidInterface instance
Where to Look First:
examples/generate.php for basic usage.ramsey/uuid, so refer to its documentation for advanced features (e.g., UUID versions, formats).Laravel Integration: For Eloquent models, configure UUIDs as primary keys:
use Illuminate\Database\Eloquent\Model;
use Broadway\UuidGenerator\UuidGenerator;
use Ramsey\Uuid\UuidInterface;
class User extends Model
{
protected $keyType = 'string';
public $incrementing = false;
protected $primaryKey = 'uuid';
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->uuid = UuidGenerator::generate()->toString();
});
}
}
UUID Generation:
UuidGenerator::generate() for default v4 UUIDs.
$uuid = UuidGenerator::generate(); // e.g., "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
$uuidString = $uuid->toString();
Testing Patterns:
$testUuid = UuidGenerator::generate(); // Same UUID across test runs (if seeded)
$this->assertEquals('expected-uuid-string', $testUuid->toString());
$this->assertInstanceOf(UuidInterface::class, $uuid);
Broadway Integration:
use Broadway\Domain\DomainMessage;
$message = new DomainMessage(
UuidGenerator::generate(),
'user_created',
['user_id' => 123]
);
Database Storage:
uuid type in migrations (PostgreSQL/MySQL 8+):
Schema::create('users', function (Blueprint $table) {
$table->uuid('uuid')->primary();
$table->string('name');
$table->timestamps();
});
protected $casts = [
'uuid' => 'string', // or use a custom accessor
];
Service Container: Bind the generator to Laravel’s container for dependency injection:
$this->app->singleton(UuidGenerator::class, function ($app) {
return new UuidGenerator();
});
Then inject it into services:
use Illuminate\Support\Facades\App;
$uuid = App::make(UuidGenerator::class)->generate();
API Responses: Return UUIDs in JSON APIs:
return response()->json([
'data' => [
'id' => $user->uuid->toString(),
'name' => $user->name,
],
]);
Event Sourcing: For Broadway, use UUIDs in event metadata:
$metadata = [
'uuid' => UuidGenerator::generate()->toString(),
'timestamp' => now()->toIso8601String(),
];
Testing Helpers: Create a test trait for reusable UUID assertions:
trait AssertsUuids
{
protected function assertValidUuid($uuid)
{
$this->assertInstanceOf(UuidInterface::class, $uuid);
$this->assertEquals(36, strlen($uuid->toString()));
}
}
PHP Version Mismatch:
Broadway-Specific Assumptions:
ramsey/uuid directly if you need broader UUID functionality.UUID Format Inconsistencies:
broadway/uuid-generator with Laravel’s Str::uuid() or symfony/uuid can cause format mismatches (e.g., hyphenated vs. non-hyphenated).Database Schema Conflicts:
string columns with a fixed length (36 chars) and validate UUID format in the application layer.Testing Determinism:
UuidGenerator::generate() may produce different UUIDs across test runs unless seeded.$this->app->instance(UuidGenerator::class, $mockUuidGenerator);
UUID Validation:
Use ramsey/uuid's validator to check UUIDs:
use Ramsey\Uuid\Validator;
$validator = new Validator();
$isValid = $validator->validate($uuidString);
Performance Bottlenecks:
Serialization Issues:
$uuidString = $uuid->toString();
$uuid = Uuid::fromString($uuidString);
Custom UUID Generators: Extend the package to support non-random UUIDs (e.g., time-based, hash-based):
use Ramsey\Uuid\Uuid;
$timeBasedUuid = Uuid::uuid1(); // Time-based UUID
Laravel Service Provider: Create a custom provider to override UUID generation:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Broadway\UuidGenerator\UuidGenerator;
class UuidServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(UuidGenerator::class, function ($app) {
return new CustomUuidGenerator(); // Your implementation
});
}
}
Testing Utilities: Add helper methods to your test suite:
// In a TestCase or trait
protected function generateTestUuid(): string
{
return UuidGenerator::generate()->toString();
}
Ramsey UUID Version:
ramsey/uuid v4.x. If you update ramsey/uuid, ensure compatibility:
composer require ramsey/uuid:^4.0
Strict Types:
declare(strict_types=1);
to files using the package.Binary UUIDs:
$binaryUuid = UuidGenerator::generate()->getBytes();
Immutable UUIDs: Treat UUIDs as immutable identifiers. Avoid regenerating them after creation.
Indexing: Ensure UUID columns are indexed in databases for performance:
$table->uuid('uuid')->primary();
$
How can I help you explore Laravel packages today?