Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
         }
     }
 }
like image 725
Tschallacka Avatar asked Aug 24 '17 14:08

Tschallacka


People also ask

Which command is used to seed your database?

The db:seed command is used to add records to a database automatically using a Seeder ( Illuminate\Database\Seeder ) class to generate or provide the records.

What is seeder migration?

Seed migration is a rails gem similar to schema migrations but for data instead. Many projects rely on some kind of initial data, a list of products for an e-commerce shop, a list of post categories in a blog, or a set of user roles, for instance. A classic pattern is to keep that data in Rails' db/seeds.


Video Answer


1 Answers

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();
    }
 }
like image 169
Desh901 Avatar answered Sep 28 '22 02:09

Desh901