How do I provide output to console when seeding or migrating tables?

一个人想着一个人 提交于 2019-12-10 18:33:04

问题


I have a plugin in october and i'm creating the neccessary tables and seeding them per the docs.

I wish to provide console output when doing that so I can debug the process i'm setting up and catching any eventualities.

How can I output information to console when running 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);
         }
     }
 }

回答1:


In your seeder you have the command property available, with the following methods available:

$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)

To see all available methods check Illuminate\Console\Command.

Example

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



回答2:


By using the Symfony class ConsoleOutput

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

$output->writeln('hello');

This will output information to console.

In the examples case

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


来源:https://stackoverflow.com/questions/45864171/how-do-i-provide-output-to-console-when-seeding-or-migrating-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!