Query \ Builder :: keyBy不存在(Laravel)

时间:2018-02-23 18:39:20

标签: laravel laravel-5.6

我已经创建了一个命令,我正在尝试查询我的数据库并通过密钥对结果进行分组,但我一直收到这个错误:

In Builder.php line 2512:

  Method Illuminate\Database\Query\Builder::keyBy does not exist. 

Laravel版本为5.6.4

命令代码:

    <?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;

class TwitchPointScanner extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:twitchPointScanner';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Twitch Point Scanner';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $usersDistributors = User::where('point_distributor', 1)
            ->inRandomOrder()
            ->get();
        $usersTwitchVerified = User::where('point_distributor', 0)
            ->whereNotNull('twitch_username')
            ->keyBy('twitch_username')
            ->get();

        $this->line('Distributors ---');
        foreach($usersDistributors as $user) {
            $this->line($user->id . ': '.$user->twitch_username);
        }

        $this->line('Point gainers ---');
        foreach($usersTwitchVerified as $user) {
            $this->line($user->id . ': '.$user->twitch_username);
        }

    }
}

1 个答案:

答案 0 :(得分:4)

keyBy是一种收集方法,因此您需要先获取数据:

$usersTwitchVerified = User::where('point_distributor', 0)
    ->whereNotNull('twitch_username')
    ->get()
    ->keyBy('twitch_username');