【问题标题】:Writing Artisan Command - Display Data from Database编写 Artisan 命令 - 显示数据库中的数据
【发布时间】:2017-12-22 21:24:33
【问题描述】:

我想为我的 Laravel Web App 编写一个 Artisan 命令。

该命令应显示数据库特定表中的记录。

我已经在 Commands 文件夹中创建了一个名为 Loginstats.php 的新命令,但我正在努力解决逻辑问题......

我认为它可以以这种方式使用:

public function handle() {
        DB::table('logincount')->orderBy ( 'customer_id' )->chunk ( 100, function ($users) {
            foreach ($users as $user) {
                //
            }
        } );
    }

我需要做什么才能从数据库中获取数据?

是否也可以使用来自Command.php 的常见工匠命令?

谢谢!

【问题讨论】:

  • 那么,如果你运行你的命令,你会看到什么?
  • [Symfony\Component\Debug\Exception\FatalErrorException] 类 'App\Console\Commands\DB' 未找到
  • @Trayer 在顶部添加use DB

标签: php laravel command laravel-artisan


【解决方案1】:

由于 DB 是一个门面,你应该使用完整的命名空间:

\DB::table...

或者在namespace子句之后添加use DB到类的顶部:

use DB;

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    相关资源
    最近更新 更多