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);
}
}
}
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.
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With