Search code examples
jsonlaravel-5controllermigrateartisan-migrate

Laravels Artisan::call('migrate:status') as json response


I want to get the migration status of my laravel app in a controller as json response and I try

$migratiorns = \Artisan::call('migrate:status');
return response()->json($migratiorns);

but \Artisan::call returns an integer 0.

What should I use for my case to get the desired response?


Solution

  • The question is quite old but I got the same problem and didn't find any solution so I made a little helper function to get pending migrations on the fly, maybe it will help someone else:

    function getPendingMigration($migrationsFolderPath = false, $toJson = false)
    {
        $migrationsFolderPath = $migrationsFolderPath ?: database_path('/migrations');
        $migrations = app('migrator')->getMigrationFiles($migrationsFolderPath);
        $pendingMigrations = [];
        foreach ($migrations as $migration => $fullpath){
            if(!\Illuminate\Support\Facades\DB::table('migrations')->where('migration', $migration)->exists())
                array_push($pendingMigrations, $migration);
        }
        return $toJson ? json_encode($pendingMigrations) : $pendingMigrations;
    }