【问题标题】:How do I provide output to console when seeding or migrating tables?播种或迁移表时如何向控制台提供输出?
【发布时间】:2018-02-02 11:41:20
【问题描述】:

我在 10 月有一个插件,我正在创建必要的表并根据文档播种它们。

我希望在执行此操作时提供控制台输出,以便调试我正在设置的进程并捕捉任何可能发生的情况。

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

【问题讨论】:

  • 你试过echo吗?

标签: laravel octobercms octobercms-plugins


【解决方案1】:

在您的播种机中,您可以使用 command 属性,并提供以下方法:

$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)

要查看所有可用方法,请查看Illuminate\Console\Command

示例

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

【讨论】:

  • 应该是的,这是我尝试的第一件事,但显然命令将在 10 月为空 [Symfony\Component\Debug\Exception\FatalThrowableError] Call to a member function info() on null 因此我自己回答了我的问题的解决方案。
  • 知道了,他们声称十月是完全可定制的,因为它基于 Laravel,但似乎他们缺少一些关于自定义组件(如 Seeders)的文档:/
  • 是的,就像对象在那里一样,我可以访问它,但它没有被填充。或者也许我只是把播种机叫错了,但是嘿,我来自 10 月份的文档基本上由“查看代码,弄清楚,看看 laravel 文档,祝你好运哟!”的那一天。所以我很高兴它已经走了多远。它非常强大,是 laravel 的一个很好的补充。
【解决方案2】:

通过使用 Symfony 类 ConsoleOutput

$output = new \Symfony\Component\Console\Output\ConsoleOutput(2);

$output->writeln('hello');

这会将信息输出到控制台。

在例子中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2019-06-04
    • 2020-04-27
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多