代码之家  ›  专栏  ›  技术社区  ›  fefe

Laravels Artisan::调用(“迁移:状态”)作为json响应

  •  2
  • fefe  · 技术社区  · 8 年前

    我想在控制器中以json响应的形式获取我的laravel应用程序的迁移状态,然后我尝试

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

    但是 \Artisan::call 返回整数 0 .

    我应该为我的案例使用什么来获得期望的响应?

    4 回复  |  直到 8 年前
        1
  •  2
  •   Kushal Billaiya    5 年前

    $migration的值将是您在命令行上看到的输出,这有点像一个表,它基本上是一个无法转换为json的字符串值。

        2
  •  1
  •   Thibaut    6 年前

    这个问题很老了,但我遇到了同样的问题,没有找到任何解决方案,所以我制作了一个小助手函数来即时获取挂起的迁移,也许它会帮助其他人:

    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;
    }
    
        3
  •  1
  •   Kai - Kazuya Ito    3 年前

    \Artisan::call('migrate:status');
    dd(Artisan::output());
    
        4
  •  0
  •   Achmadi Mas Adi    5 年前

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

    ini句柄函数

    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');
        }
    }
    

    就这么跑吧

    php artisan command:name -json