在播种或迁移表时如何向控制台提供输出?

问题描述 投票:0回答:2

我十月份有一个插件,我正在创建必要的表并根据文档播种它们。

我希望在执行此操作时提供控制台输出,以便我可以调试我正在设置的进程并捕获任何意外情况。

运行时如何向控制台输出信息

php artisan october:up

use Db;
use Seeder;

class SeedGeoStateTable extends Seeder
{
    public function run()
     {
     foreach(array_merge(glob(__DIR__.'/seed/geo_state/*.txt'), glob(__DIR__.'/seed/geo_state/*.json')) as $file) {
           $this->insert($file);     
           gc_collect_cycles();
        }
     }

     public function insert($file) {
         // output to console which file i'm seeding here
         $json = json_decode(file_get_contents($file),true);
         foreach($json as $entry) {
             Db::table("geo_state")->insert($entry);
         }
     }
 }
php laravel octobercms octobercms-plugins
2个回答
47
投票

在您的播种器中,您可以使用

command
属性,并可以使用以下方法:

$this->command->info($message)
$this->command->line($message)
$this->command->comment($message)
$this->command->question($message)
$this->command->error($message)
$this->command->warn($message)
$this->command->alert($message)

要查看所有可用的方法,请检查

Illuminate\Console\Command

示例

public function run()
 {
 $this->command->comment('Seeding GeoState...');
 foreach(array_merge(glob(__DIR__.'/seed/geo_state/*.txt'), glob(__DIR__.'/seed/geo_state/*.json')) as $file) {
       $this->insert($file);     
       gc_collect_cycles();
    }
 }

-1
投票

通过使用 Symfony 类 ConsoleOutput

$output = new \Symfony\Component\Console\Output\ConsoleOutput(2);

$output->writeln('hello');

这会将信息输出到控制台。

在示例中

use Db;
use Seeder;

class SeedGeoStateTable extends Seeder
{
    public function run()
     {
     foreach(array_merge(glob(__DIR__.'/seed/geo_state/*.txt'), glob(__DIR__.'/seed/geo_state/*.json')) as $file) {
           $this->insert($file);     
           gc_collect_cycles();
        }
     }

     public function insert($file) {
         // output to console which file i'm seeding here
         $output = new \Symfony\Component\Console\Output\ConsoleOutput(2);
         $output->writeln("Seeding table with file $file");
         $json = json_decode(file_get_contents($file),true);
         foreach($json as $entry) {
             Db::table("geo_state")->insert($entry);
         }
     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.