laravel console - 自定义命令

在改造一个支付流程,新的流程加入了一个新的数据表字段,但是这个新的字段需要通过计算来填充,所以为了兼容历史数据,必须将已有的数据行重新计算一遍该字段。

这时使用 laravel console 命令就非常方便,因为可以共用 .env 中的数据库连接配置,以及定义好的 model。

自动创建 console 命令类

php artisan make:command YourCommand

这时会看到目录

app/Console/Commands

下多了一个新的命令类,在 handle 中写计算逻辑即可。

console 命令类的几个必填项

  • signature 命令名及参数指定
  • description 命令描述
  • handle 即逻辑部分

在 console 中输出日志

$this->info('Display this on the screen');

参考

https://laravel.com/docs/5.6/artisan#introduction

posted @ 2018-12-29 16:10  心之所依  阅读(918)  评论(0编辑  收藏  举报