12

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);
         }
     }
 }
Tschallacka
  • 27,901
  • 14
  • 88
  • 133

2 Answers2

45

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();
    }
 }
Desh901
  • 2,633
  • 17
  • 24
  • Should yes, that's the first thing I tried, but aparently command will be null on october `[Symfony\Component\Debug\Exception\FatalThrowableError] Call to a member function info() on null` Hence my self answered solution to my problem. – Tschallacka Aug 25 '17 at 06:13
  • 2
    Got it, they claim that October is fully customizable because it's based on Laravel, but it seems like they are missing some documentation on custom components like Seeders :/ – Desh901 Aug 25 '17 at 08:40
  • yea, like the object is there, I can access it, but it's not populated. Or maybe i'm just calling the seeder wrong, but hey, I come from the day that docs of october consisted basically of "Look at the code, figure it out, look at laravels docs, good luck yo!" so i'm happy with as how far it has come. It's very powerfull and a nice additon to laravel. – Tschallacka Aug 25 '17 at 08:57
-1

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);
         }
     }
 }
Tschallacka
  • 27,901
  • 14
  • 88
  • 133