👨🏻‍💻 Thanks for reading! This blog has been archived and won't have content updates. You can follow my latest work at trujillo.io.
CHROMABITS

Eduardo Trujillo
1 minute

Found a quick way of avoiding using the Schema facade for migrations in Laravel:

<?php

namespace App\Database;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Builder;

/**
 * Class BaseMigration
 *
 * Base migration class
 *
 * @package App\Database
 */
abstract class BaseMigration extends Migration
{
    /**
     * @var Builder
     */
    protected $builder;

    /**
     * Construct an instance of a BaseMigration
     */
    public function __construct()
    {
        $this->builder = app('db')
            ->connection($this->connection)
            ->getSchemaBuilder();
    }
}

Why? This should help with the type hinting provided by some IDEs such as PHPStorm.

To use this class, extend it from every migration you make and replace any mention of Schema:: with just $this->builder->

CHROMABITS
Copyright © 2015-2021 - Eduardo Trujillo
Except where otherwise noted, content on this site is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.
Site generated using Gatsby.