10

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

我希望在这样做时提供控制台输出,这样我就可以调试我正在设置的过程并捕捉任何可能发生的事情。

运行时如何将信息输出到控制台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);
         }
     }
 }
4

2 回答 2

29

在您的播种机中,您有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();
    }
 }
于 2017-08-24T14:36:30.243 回答
0

通过使用 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);
         }
     }
 }
于 2017-08-24T14:24:18.117 回答