1

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?

fefe
  • 8,755
  • 27
  • 104
  • 180

4 Answers4

1

The value of $migration will be the output you see on the command line, which is sort of a table, its basically a string value which cannot be converted to json.

Kushal Billaiya
  • 444
  • 1
  • 8
  • 21
1

An easy way is the following

\Artisan::call('migrate:status');
dd(Artisan::output());
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Kareem Essawy
  • 565
  • 1
  • 4
  • 14
0

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;
}
Thibaut
  • 266
  • 3
  • 10
0

Here ini my console code

protected $signature = 'command:name {json?}';

ini handle function

public function handle()
{
    $json = $this->argument('json');
    if($json){
        $a = ['foo'=>
            ['bar', $json]
        ];
        $a = collect($a);
        $this->info($a->toJson());
    } else {
        $this->info('Display this on the screen');
    }
}

just run

php artisan command:name -json 

will get json output