jarrodtomholt/belongs-to-macro

Automatically add index and constraints to your migration foreignIdFor entities with a simple macro

1.0.0 2023-02-14 20:08 UTC

This package is auto-updated.

Last update: 2025-06-16 13:33:56 UTC


README

Automatically add index and foreign key constraints to your migrations enforcing data integrity, preserving the referential association and improving query performance.

Installation

You can install the package via composer:

composer require jarrodtomholt/belongs-to-macro

Usage

In a migrations up() method, use the following in place of foreignIdFor to add indexes and foreign key constraints.

Schema::create('posts', function (Blueprint $table) {
    $table->belongsTo(User::class);
});

// will expand to
$table->foreignIdFor(User::class)->index()->constrained();

Need a nullable field? use belongsToOrNull

Schema::create('posts', function (Blueprint $table) {
    $table->belongsToOrNull(User::class);
});

// will expand to
$table->foreignIdFor(User::class)->nullable()->index()->constrained();

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please raise an issue using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.