mindtwo/laravel-auto-create-uuid
Auto-fill a UUID v4 on Eloquent models when creating or replicating. Add a trait, add a uuid column, and it just works—no config. Supports custom UUID column names and ensures replicas get a fresh UUID by excluding the UUID attribute on replicate.
Pros:
creating, replicating) to auto-generate UUIDs, reducing boilerplate. This aligns well with Laravel’s convention-over-configuration philosophy.replicate(), a critical use case for distributed systems or model cloning.$uuid_column property or getUuidColumn() method, accommodating varying database schemas.Cons:
Str::isUuid(): Relies on Laravel’s built-in validation, which may not match custom UUID validation logic in edge cases.ramsey/uuid dependency (removed in v3.0), reducing bloat.uuid() column in migrations. Risk: Retrofitting UUIDs to existing tables may require downtime or complex migrations (e.g., adding a column with a default value).creating and replicating events. Risk: Potential conflicts if other packages or custom code override these events or the replicate() method.binary(16) in MySQL for faster lookups).Model::create([...])) may not benefit from this package’s auto-generation.binary(16) for UUID columns in MySQL/PostgreSQL for performance.User, Order)?id for joins, uuid for APIs)?replicate() heavily used in the codebase? If not, the replication feature may be overkill.Str::isUuid()?uuid() column to migrations for target models. Example:
$table->uuid('uuid')->unique()->after('id'); // Preserve auto-increment for joins
Schema::table('users', function (Blueprint $table) {
$table->uuid('uuid')->unique()->after('id');
$table->index('uuid');
});
AutoCreateUuid trait to target models.$uuid_column if needed (e.g., protected string $uuid_column = 'external_id';).creating and replicating scenarios.id in favor of UUIDs for APIs, while keeping it for internal joins.index('uuid')) and use binary(16) in MySQL for optimal lookup speed.Log, Audit) to validate the integration.User, Product) after confirming stability.composer.json to avoid unexpected updates.Model::insert()) will not auto-generate UUIDs; handle manually or via post-insert triggers.binary(16) in MySQL) to avoid performance degradation.id for joins).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid UUID pre-populated | Model creation fails | Validate UUIDs in model boot() or API layer. |
| Event listener conflicts | UUID not generated or duplicated | Test with existing creating/replicating hooks. |
| Database UUID index corruption | Slow queries or failures |
How can I help you explore Laravel packages today?