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());
@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