Skip to content

Instantly share code, notes, and snippets.

@jaceju
Created March 9, 2018 03:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaceju/cc53d2fbab6e828f69b2a3b7e267d1ed to your computer and use it in GitHub Desktop.
Save jaceju/cc53d2fbab6e828f69b2a3b7e267d1ed to your computer and use it in GitHub Desktop.
Use Laravel Migration standalone
<?php
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Events\Dispatcher;
require __DIR__ . '/vendor/autoload.php';
/**
* 設定資料庫連線
*/
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$capsule->setEventDispatcher(new Dispatcher(new Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();
/**
* 初始化必要參數
*/
$container = Container::getInstance();
$databaseMigrationRepository = new DatabaseMigrationRepository($capsule->getDatabaseManager(), 'migration');
$databaseMigrationRepository->createRepository();
$container->instance(MigrationRepositoryInterface::class, $databaseMigrationRepository);
$container->instance(ConnectionResolverInterface::class, $capsule->getDatabaseManager());
/**
* 執行 migration
*/
$paths = [
__DIR__ . '/migrations',
];
/** @var Migrator $migrator */
$migrator = $container->make(Migrator::class);
$migrator->run($paths);
var_dump($migrator->getNotes());
/**
* 執行 rollback
*/
$migrator->rollback($paths);
var_dump($migrator->getNotes());
@31337Ghost
Copy link

31337Ghost commented Apr 8, 2019

Update:
Got it work using Capsule::schema() calls Capsule::schema()->create() against of Facaded Schema::create(); in migration file.

class CreateEquipmentTable extends Migration
{
    public function up()
    {
        Capsule::schema()->create('equipment', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tree_left')->default(0);
            $table->integer('tree_right')->default(0);
            $table->integer('tree_level')->default(0);
        });
    }

    public function down()
    {
        Capsule::schema()->dropIfExists('equipment');
    }
}

Original question:
Using

  "require": {
    "illuminate/database": "^5.7",
    "illuminate/pagination": "^5.7",
    "illuminate/filesystem": "^5.7",
    "illuminate/events": "^5.8"
  },

Tried your code with migration file:

class CreateEquipmentTable extends Migration
{
    public function up()
    {
        Schema::create('equipment', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tree_left')->default(0);
            $table->integer('tree_right')->default(0);
            $table->integer('tree_level')->default(0);
        });
    }
}

getting error


Fatal error: Uncaught Error: Call to a member function connection() on null in /var/www/html/billing/vendor/illuminate/support/Facades/Schema.php:36 

Stack trace: 
#0 /var/www/html/billing/vendor/illuminate/support/Facades/Facade.php(144): Illuminate\Support\Facades\Schema::getFacadeAccessor() 
#1 /var/www/html/billing/vendor/illuminate/support/Facades/Facade.php(231): Illuminate\Support\Facades\Facade::getFacadeRoot() 
#2 /var/www/html/billing/src/vendor/Billing/database/migrations/2019_04_07_094203_create_equipment_table.php(125): Illuminate\Support\Facades\Facade::__callStatic('create', Array) 
#3 /var/www/html/billing/vendor/illuminate/database/Migrations/Migrator.php(360): CreateEquipmentTable->up() 
#4 /var/www/html/billing/vendor/illuminate/database/Migrations/Migrator.php(367): Illuminate\Database\Migrations\Migrator->Illuminate\Database\Migrations\{closure}() 
#5 /var/www/html/billing/vendor/illuminate/database/Migrations/Migrator.php(178): Illuminate\Database\Migrations\Migrator->runMigration(Object(CreateEquipmentTabl in /var/www/html/billing/vendor/illuminate/support/Facades/Schema.php on line 36

Error comes from /var/www/html/billing/vendor/illuminate/support/Facades/Schema.php:36

    /**
     * Get a schema builder instance for the default connection.
     *
     * @return \Illuminate\Database\Schema\Builder
     */
    protected static function getFacadeAccessor()
    {
        return static::$app['db']->connection()->getSchemaBuilder();
    }

adding

var_dump($app['db']);

gives NULL

Maybe I should use something other than "Schema::create()" in my migrations files? Or need to "$container->instance()" something more than in tour gist?

Thank you in advance.

@iamrekas
Copy link

This problem can be solved by adding 2 lines:

on the top:
use Illuminate\Support\Facades\Schema;

and in the init function at the end:

Schema::setFacadeApplication([
    'db' => $this->capsule
]);

this is how you can hack some fascades, in additional funcionality of others fascades made little proxy for DB class:

class MigrationUtils {
    static function raw($value) {
        return new \Illuminate\Database\Query\Expression($value);
    }
}

and it using in migrations as DB class (example for raw function)

@jigneshkumarmistry
Copy link

@31337Ghost I have try your code. It's seems working solutions for the first attempt. But when you run again it is throwing error for the migration table that it's already exits. I am looking for the solutions for this. If you can help me out.

@kWeb24
Copy link

kWeb24 commented Sep 14, 2020

@31337Ghost I have try your code. It's seems working solutions for the first attempt. But when you run again it is throwing error for the migration table that it's already exits. I am looking for the solutions for this. If you can help me out.

You have to check if migrations table already exists. Use repositoryExists() method in DatabaseMigrationRepository just like that:

if (!$databaseMigrationRepository->repositoryExists()) {
    $databaseMigrationRepository->createRepository();
}

@jigneshkumarmistry
Copy link

@kWeb24 Thank you for the solutions. This work 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment