Laravel 13.25 adds foreignUlidFor() to the migration Blueprint, allowing you to define ULID foreign key columns and table constraints directly from model classes.
When building applications where models use ULIDs for primary keys, setting up foreign key columns in database migrations previously required declaring both the ULID column and its foreign constraint manually.
Laravel 13.25 introduces the foreignUlidFor() schema helper on the Illuminate\Database\Schema\Blueprint class. This method automatically creates the ULID column and establishes the foreign key constraint based on the given model.
Before and After
Before Laravel 13.25
Defining a ULID foreign key required manually declaring the ULID column and chaining foreign key constraints:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::create('posts', function (Blueprint $table) {
$table->id();
// Manual ULID column and foreign key constraint
$table->ulid('user_id');
$table->foreign('user_id')
->references('id')
->on('users');
$table->timestamps();
});
With foreignUlidFor() in Laravel 13.25
Using foreignUlidFor(), the column name and table constraint are resolved directly from the model class:
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::create('posts', function (Blueprint $table) {
$table->id();
// Creates user_id ULID column and foreign key constraint to users table
$table->foreignUlidFor(User::class);
$table->timestamps();
});
How It Works
Passing a model class (such as User::class) to foreignUlidFor() performs two steps during migration execution:
- Creates a ULID column matching the conventional model foreign key name (
user_id). - Configures the foreign key constraint referencing the
idcolumn on the associated model table (users).
The helper assumes that the referenced model uses ULIDs for its primary keys.
Key Considerations
- Migration Schema Helper Only:
foreignUlidFor()is a database schema helper for migration files. It does not define Eloquent model relationships or inverse relationship methods on Eloquent model classes. - New Migrations Scope: This helper applies when defining new tables or altering schemas inside active migration files. It does not automatically modify existing database tables or retroactively update previously executed migrations.
- Convention Driven: The column name and table target are derived from standard model conventions (
Userresolves touser_idand tableusers).
Related Tips
View all tips →Reusable Migration Fields with Custom Blueprint Macros
Extend Laravel's Blueprint class with custom macros to standardize and reuse common schema columns across database migrations.
Refresh and Pessimistically Lock Models with refreshForUpdate() in Laravel 13.27
Laravel 13.27 adds refreshForUpdate(), reloading an existing model instance in-place with fresh database attributes while acquiring an exclusive FOR UPDATE row lock.