-
-
Save jaceju/cc53d2fbab6e828f69b2a3b7e267d1ed to your computer and use it in GitHub Desktop.
<?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()); |
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)
@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.
@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();
}
@kWeb24 Thank you for the solutions. This work 👍
Update:
Got it work using
Capsule::schema()
callsCapsule::schema()->create()
against of FacadedSchema::create();
in migration file.Original question:
Using
Tried your code with migration file:
getting error
Error comes from /var/www/html/billing/vendor/illuminate/support/Facades/Schema.php:36
adding
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.