在Laravel 5.4中缺少Illuminate \ Support \ Collection :: where()的参数2

时间:2017-06-28 07:34:52

标签: php laravel laravel-5 laravel-5.4

我正在Laravel 5.4中构建一个小应用程序,我正在进行以下查询:

$user = User::find(1);
$tasks = $user->tasks;
$tasks->count = $tasks->count();
$tasks->completedCount = $tasks->where('status', '=', 'Closed')->count();
$tasks->investorCount = $tasks->where('task_id', '=', 'Investor Targets')->count();
$tasks->investorCompletedCount = $tasks->where([
    ['task_id', '=', 'Investor Targets'],
    ['status', '=', 'Closed'],
])->get()->count();
$tasks->researchCount = $tasks->where('task_id', '=', 'Research Targets')->count();
$tasks->researchCompletedCount = $tasks->where([
    ['task_id', '=', 'Research Targets'],
    ['status', '=', 'Closed'],
])->get()->count();
dd($tasks);

我收到了以下错误;

  

缺少Illuminate \ Support \ Collection :: where()

的参数2

在第

$tasks->investorCompletedCount = $tasks->where([
        ['task_id', '=', 'Investor Targets'],
        ['status', '=', 'Closed'],
    ])->get()->count();

我的语法也正确,我不知道这个问题会在哪里发生。帮助我解决这个问题。

3 个答案:

答案 0 :(得分:5)

此代码

$tasks->investorCompletedCount = $tasks->where([
    ['task_id', '=', 'Investor Targets'],
    ['status', '=', 'Closed'],
])->get()->count();

需要重写为

$tasks->investorCompletedCount = $tasks->where('task_id', 'Investor Targets')
->where('status', 'Closed')->count();

问题是您混淆了where

的方法Eloquent query construction

https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_where

使用where

的方法Collection

https://laravel.com/api/5.4/Illuminate/Support/Collection.html#method_where)。

实际上,您尝试使用Collection,就好像它是query builder,因为您还尝试执行get

答案 1 :(得分:1)

  

您正在将android studioEloquent where混合。

将您的代码更改为: -

Collection where

答案 2 :(得分:1)

你可以这样使用

$tasks->investorCompletedCount = $tasks
->where(['task_id' => 'Investor Targets','status' => 'Closed'])
->get()
->count();