gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:26:31 26 4
gpt4 key购买 nike

我创建了一个命令,并尝试查询我的数据库并按键对结果进行分组,但我不断收到此错误:

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

}
}

最佳答案

keyBy是一个集合方法,所以需要先获取数据:

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

关于laravel - Query\Builder::keyBy 不存在 (Laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48954291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com