Skip to content

Getting Started

Eliminate repetitive match expressions in your Laravel migrations with type-safe Blueprint macros for variable primary keys, foreign keys, and polymorphic relationships.

Variable Keys v1.0 requires PHP 8.5+ and Laravel 12+.

Install Variable Keys with composer:

Terminal window
composer require cline/variable-keys

The package will automatically register its service provider and Blueprint macros.

Instead of writing verbose match expressions in migrations:

use Cline\VariableKeys\Enums\PrimaryKeyType;
Schema::create('users', function (Blueprint $table) {
// Before: verbose match expression
match (PrimaryKeyType::ULID) {
PrimaryKeyType::ULID => $table->ulid('id')->primary(),
PrimaryKeyType::UUID => $table->uuid('id')->primary(),
PrimaryKeyType::ID => $table->id(),
};
$table->string('name');
$table->timestamps();
});

Use clean, type-safe macros:

use Cline\VariableKeys\Enums\PrimaryKeyType;
Schema::create('users', function (Blueprint $table) {
// After: clean macro
$table->variablePrimaryKey(PrimaryKeyType::ULID);
$table->string('name');
$table->timestamps();
});

Register models in your service provider to enable automatic primary key generation:

use Cline\VariableKeys\Facades\VariableKeys;
use Cline\VariableKeys\Enums\PrimaryKeyType;
use App\Models\User;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
VariableKeys::map([
User::class => [
'primary_key_type' => PrimaryKeyType::ULID,
],
]);
}
}

Add the trait to your models:

use Cline\VariableKeys\Database\Concerns\HasVariablePrimaryKey;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasVariablePrimaryKey;
}
## What's Included
Variable Keys provides three Blueprint macros and two enums:
### Enums
- **`PrimaryKeyType`** - ID, ULID, UUID
- **`MorphType`** - String, Numeric, UUID, ULID
### Blueprint Macros
- **`variablePrimaryKey()`** - Primary key columns
- **`variableForeignKey()`** - Foreign key columns
- **`variableMorphs()`** - Polymorphic relationship columns
## Next Steps
- [Primary Keys](primary-keys.md) - Configure variable primary keys
- [Foreign Keys](foreign-keys.md) - Manage foreign key relationships
- [Polymorphic Relations](polymorphic-relations.md) - Handle polymorphic relationships
- [Configuration Patterns](configuration-patterns.md) - Centralize key type configuration