symfony/uid
Symfony UID component offers an object-oriented API to generate and work with unique identifiers. Includes ULIDs and UUIDs (v1 and v3–v8), with implementations compatible with both 32-bit and 64-bit systems for consistent, portable IDs.
Strong Fit for Laravel:
UUID, MySQL BINARY(16)), aligning with Laravel’s modern database practices.Key Synergies:
incrementing IDs with Uuid traits or custom accessors (e.g., $model->getRouteKey()).Uuid::toRfc9562() for RFC-compliant payloads (e.g., GraphQL, JSON:API).MockUuidFactory integrates with Laravel’s testing tools (Pest, PHPUnit) for deterministic UUIDs in CI.Anti-Patterns to Avoid:
ALTER TABLE users ADD COLUMN id BINARY(16)).Laravel Ecosystem Compatibility:
BLOB), and SQL Server.Dependency Conflicts:
ext-ds for Hashable in some cases).ramsey/uuid (use one or the other).symfony/polyfill versions in composer.json.Customization Points:
UuidFactory or UlidFactory for custom logic (e.g., namespace prefixes).Uuid::isValid() in Laravel’s FormRequest or Validator.getType() in Eloquent models for binary storage.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Database Migration | High | Test binary storage performance in staging; use ALTER TABLE with backups. |
| Backward Compatibility | Medium | Phase adoption (e.g., dual id and uuid columns during transition). |
| Testing Overhead | Low | Leverage MockUuidFactory to reduce flaky tests. |
| Performance Impact | Low | Benchmark binary vs. string storage; UUIDv7 adds negligible overhead (~5µs per gen). |
| Team Adoption | Medium | Provide migration guides, CLI tools, and pair programming for critical paths. |
| Vendor Lock-in | Low | UID is a standard-compliant component; switching to ramsey/uuid is trivial. |
Laravel-Specific Integrations:
use Symfony\Uid\Uuid;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $keyType = 'string';
protected $incrementing = false;
protected $casts = ['id' => Uuid::class];
protected static function boot() {
parent::boot();
static::creating(function ($model) {
$model->id = Uuid::v7(); // or Uuid::v4(), Ulid::generate()
});
}
}
use Symfony\Uid\Uuid;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource {
public function toArray($request) {
return [
'id' => $this->resource->id->toRfc9562(), // RFC 9562 format
'name' => $this->name,
];
}
}
use Symfony\Uid\Uuid;
use Illuminate\Validation\Rule;
$request->validate([
'uuid' => ['required', Rule::function('uuid')->make(function ($attribute, $value) {
return Uuid::isValid($value);
})],
]);
Symfony Ecosystem:
@Groups and normalization contexts for API responses.Phase 1: Pilot Project
id in analytics_events table with ULID.Phase 2: Core Models
users, orders) with dual-column support:
ALTER TABLE users ADD COLUMN uuid BINARY(16);
UPDATE users SET uuid = UNHEX(REPLACE(id, '-', ''));
Uuid trait or custom accessors.Phase 3: Full Adoption
| Component | Compatibility | Notes |
|---|---|---|
| Eloquent | Full support (binary storage, casting, accessors) | Use protected $keyType = 'string' for UUIDs. |
| Laravel Scout | Partial (custom searchable attributes) | Use toBase58() for compact indexing. |
| Redis/Memcached | High (binary storage reduces payload size) | Serialize with Uuid::toBinary(). |
| API Platform | Full (native Symfony integration) | Use UuidNormalizer. |
| Laravel Nova | Manual (custom resource fields) | Extend NovaResource for UID display. |
| Laravel Echo | Full (WebSocket channel IDs) | Use Uuid::v7() for time-ordered channels. |
php artisan make:migration add_uuid_to_users_table --table=users
public function up() {
Schema::table('users', function (Blueprint $table) {
$table->binary('uuid')->after('id')->unique();
});
}
User::chunk(1000, function ($users) {
foreach ($users as $user) {
$user->uuid = Uuid::fromString($user->id)->toBinary();
$user->save();
}
});
class User extends Model {
protected $keyType = 'string';
public $incrementing = false;
protected $casts = ['uuid' => Uuid::class];
}
scopeByLegacyId() for temporary compatibility.id column in a future migration.How can I help you explore Laravel packages today?