【问题标题】:Laravels Artisan::call('migrate:status') as json responseLaravel Artisan::call('migrate:status') 作为 json 响应
【发布时间】:2017-10-15 19:00:16
【问题描述】:

我想在控制器中获取我的 laravel 应用程序的迁移状态作为 json 响应,我尝试

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

但是\Artisan::call 返回一个整数0

我应该使用什么来获得所需的响应?

【问题讨论】:

    标签: json laravel-5 controller migrate artisan-migrate


    【解决方案1】:

    $migration 的值将是你在命令行上看到的输出,它是一个表格,它基本上是一个不能转换为 json 的字符串值。

    【讨论】:

      【解决方案2】:

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

      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】:

        这里是我的控制台代码

        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 
        

        会得到json输出

        【讨论】:

          【解决方案4】:

          一个简单的方法如下

          \Artisan::call('migrate:status');
          dd(Artisan::output());
          

          【讨论】:

            猜你喜欢
            • 2016-08-31
            • 2019-06-05
            • 1970-01-01
            • 2017-01-07
            • 2014-08-29
            • 2018-08-18
            • 2016-10-05
            • 2014-08-05
            • 2021-01-24
            相关资源
            最近更新 更多