eig/uuid
eig/uuid provides UUID generation and helper utilities for Laravel/PHP applications. It simplifies creating unique identifiers and integrating UUIDs into models, database records, and application workflows, making it a focused option for projects that prefer UUID-based IDs.
composer require webpatser/laravel-uuid. Laravel 5.5+ auto-discovers Webpatser\Uuid\UuidServiceProvider and the Uuid facade alias; on older versions register both in config/app.php.Uuid class source and the README method table — the class is small and the public API is the whole story.use Webpatser\Uuid\Uuid;
$id = Uuid::generate(4)->string;
if (! Uuid::validate($request->input('id'))) {
abort(422, 'Invalid identifier');
}
Uuid::validate(string): bool) is the lowest-friction entry point — no facade, no config, no DB round-trip. Reach for it before writing another UUID regex.$incrementing = false and $keyType = 'string', then assign in a creating hook:
protected static function booted(): void
{
static::creating(fn ($m) => $m->id ??= Uuid::generate(4)->string);
}
Validator::extend('uuid', fn ($attr, $value) => Uuid::validate($value));
Then 'id' => 'required|uuid' in FormRequests. Keep the closure thin — validate() already handles format checking.Uuid::validate() in middleware or a route constraint before binding, so malformed IDs return 404/422 rather than triggering a DB lookup with garbage.Uuid::validate($raw) first, then Uuid::import($raw) and check its boolean return. The two-step pattern separates "wrong shape" from "wrong version/bits."$table->uuid('id')->primary() in migrations — an accidental integer cast silently truncates.Uuid::generate(4)->string; if you only ever need v4, Laravel's Str::uuid() is a lighter alternative, but this package earns its place when you need v1/v3/v5.Uuid::validate($model->id) instead of hand-rolled regexes — one dependency, clearer intent, and it tracks format changes automatically.Uuid::generate() returns an object, not a string. You must call ->string or cast. This is the most common mismatch with people coming from Str::uuid().validate() is format-strict. If your inputs can arrive with braces, uppercase hex, or urn:uuid: prefixes, normalize with trim()/strtolower() first or it will return false.generate(1) is time-based and leaks creation order/timing; generate(4) is random. Default to v4 unless you specifically need sortable or namespace UUIDs.Uuid facade alias can collide with other packages. In shared libraries, import the FQCN rather than relying on the alias.:attribute interpolation — wrap it as shown above.Uuid or bind your own resolver if you need to accept non-standard formats (braced UUIDs, mixed case) without forking the package.How can I help you explore Laravel packages today?