问题
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