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

Tsc*_*cka 3 laravel octobercms octobercms-plugins

我有一个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);
         }
     }
 }
Run Code Online (Sandbox Code Playgroud)

Des*_*901 14

在您的播种机中,您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)
Run Code Online (Sandbox Code Playgroud)

要查看所有可用方法,请查看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();
    }
 }
Run Code Online (Sandbox Code Playgroud)

  • 明白了,他们声称 October 是完全可定制的,因为它基于 Laravel,但似乎他们缺少一些关于 Seeders 等定制组件的文档:/ (2认同)

Tsc*_*cka -1

通过使用 Symfony 类 ConsoleOutput

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

$output->writeln('hello');
Run Code Online (Sandbox Code Playgroud)

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

在示例情况下

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);
         }
     }
 }
Run Code Online (Sandbox Code Playgroud)