Skip to content

Share common database columns across multiple tables in your Laravel migrations

Posted on:09/12/2022

Laravel’s Illuminate\Database\Schema\Blueprint class, commonly used in migrations, uses the \Illuminate\Support\Traits\Macroable trait.

Rather than duplicating code across multiple migrations we can register and use custom macros to extend the functionality of the Illuminate\Database\Schema\Blueprint class.

// The `boot` method in one of your app's service providers is a sensible place to register macros.

use Illuminate\Database\Schema\Blueprint;

Blueprint::macro('isFeaturable', function () {
    /** @var Blueprint $this */
    $this->boolean('featured')->default('0')->index();
});
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Schema::create('jobs', function (Blueprint $table) {
    $table->isFeaturable();
});

Schema::create('companies', function (Blueprint $table) {
    $table->isFeaturable();
});

This pairs nicely with model traits declaring query scopes.

use Illuminate\Database\Eloquent\Builder;

trait FeaturableTrait
{
    public function scopeFeatured(Builder $query): Builder
    {
        return $query->where('featured', '1');
    }
    
    // ...
}
$jobs = \App\Models\Job::featured()->get();
$companies = \App\Models\Company::featured()->get();