cline/variable-keys
Laravel Blueprint macros for variable primary key types, matching foreign keys, and polymorphic morph types. Easily switch between id/uuid/ulid (and more) via enums/config to keep migrations consistent across models and relationships.
morphTo/morphWith) where key types (e.g., UUID vs. integer) historically required manual match logic or custom morph maps. The MorphType enum ensures alignment across tables.switch/match blocks in migrations, improving readability and maintainability. Critical for teams with frequent schema changes or multi-developer workflows..env) without migration rewrites, supporting feature flags or A/B testing of key strategies (e.g., UUIDs for security vs. integers for performance).UUID vs. MySQL’s CHAR(36)).variablePrimaryKey() without affecting existing migrations. Legacy tables may require retrofitting (e.g., adding UUID columns via Schema::table()).getKey(), getIncrementing()), but may need custom model bindings for legacy assumptions (e.g., Model::increment()). Polymorphic relationships benefit from built-in MorphType support.ramsey/uuid), but assumes the underlying database supports the chosen type. PostgreSQL is ideal; MySQL/SQLite may need workarounds (e.g., CHAR(36) for UUIDs).variablePrimaryKey(), variableForeignKey()) may conflict with custom Blueprint extensions or third-party packages modifying Blueprint (e.g., spatie/laravel-activitylog).morphMap or MorphTo resolution.Model::find(1)) or ORM-specific features (e.g., Model::increment()) will need adjustments.spatie/laravel-permission) may not handle variable key types out of the box.Schema::table() vs. new columns)?spatie/laravel-permission) that may conflict with variable key types?morphMap be updated to reflect variable key types (e.g., user → User::class with UUID PK)?CHAR(36) for UUIDs) be handled?getKey(), getIncrementing()), but may require custom model bindings for legacy assumptions.UUID type natively.CHAR(36) for UUIDs or BINARY(16) for binary UUIDs. May need custom key generation logic.TEXT for UUIDs) or avoidance.postgres:15-alpine for UUIDs, mysql:8.0 with CHAR(36)).ramsey/uuid or webmozart/assert under the hood. No additional dependencies required unless custom generation is needed.Phase 1: Configuration (Pre-Migration)
config/database.php with primary_key_type (e.g., uuid, string, integer).
'primary_key_type' => env('DB_PRIMARY_KEY_TYPE', 'uuid'),
php artisan vendor:publish --tag="variable-keys-config"
.env with the desired key type:
DB_PRIMARY_KEY_TYPE=uuid
Phase 2: New Tables (Immediate Adoption)
->id() with ->variablePrimaryKey() in new migrations.
Schema::create('users', function (Blueprint $table) {
$table->variablePrimaryKey(PrimaryKeyType::UUID);
$table->string('name');
$table->timestamps();
});
variableMorphs():
Schema::create('comments', function (Blueprint $table) {
$table->variablePrimaryKey(PrimaryKeyType::UUID);
$table->variableMorphs(['commentable_id', 'commentable_type']);
$table->text('body');
});
Phase 3: Foreign Keys (Consistent Relationships)
variableForeignKey() to ensure foreign keys match the primary key type:
Schema::create('posts', function (Blueprint $table) {
$table->variable
How can I help you explore Laravel packages today?