framework icon indicating copy to clipboard operation
framework copied to clipboard

Add configurable default id type for schema builder

Open mohamedsharaf opened this issue 3 months ago • 5 comments

Description

This PR introduces a new static property on the schema Builder to configure the default column type used by $table->id().

Currently, $table->id() always produces a bigIncrements column. This is fine for many applications, but more and more projects default to UUID or ULID for their primary keys. Today, the only way to achieve that is to either:

  • Manually call $table->uuid('id')->primary() or $table->ulid('id')->primary() in every migration, or
  • Register a custom Blueprint macro in each project.

Changes in this PR

  • Added Builder::$defaultIdType (similar to the existing Builder::$defaultMorphKeyType).
  • Default remains bigIncrements for full backwards compatibility.
  • Supported values: bigIncrements, uuid, ulid.

When changed, $table->id() will respect this global default.

Example

use Illuminate\Database\Schema\Builder;

// Globally set default id type for migrations
Builder::$defaultIdType = 'ulid';

Now, any migration that calls:

Schema::create('users', function (Blueprint $table) {
    $table->id(); // ULID by default
});

Will produce:

create table `users` (
    `id` char(26) not null primary key
);

Motivation

  • Provides consistency with $defaultMorphKeyType.
  • Simplifies migrations for teams using UUID/ULID.
  • Keeps migration files cleaner and more expressive without boilerplate.

Backward Compatibility

  • The default remains bigIncrements, so no breaking changes.
  • Existing migrations and behavior remain unchanged unless developers explicitly configure Builder::$defaultIdType.

Community Tip

If you want all of your migrations to default to ULID or UUID primary keys, you can configure this once in your AppServiceProvider

use Illuminate\Database\Schema\Builder;

/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    // Use ULIDs as default id type
    Builder::$defaultIdType = 'ulid';

    // Or, if you prefer UUIDs:
    // Builder::$defaultIdType = 'uuid';
}

Now, every $table->id() call in your migrations will automatically create the correct column type without needing to repeat $table->uuid('id')->primary() or $table->ulid('id')->primary() everywhere.

mohamedsharaf avatar Aug 25 '25 12:08 mohamedsharaf