phplaraveloctobercmsoctobercms-plugins

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


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

Solution

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