缺少参数2 for Laravel 5.4中的Laravel support support collect



我正在在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);

我正在遵循错误;

缺少参数2 for Illuminate support collection :: where((

在行中

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

我的语法也是正确的,我不知道这个问题的到来。在此帮助我。

此代码

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

需要重写为

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

问题是您正在混淆Eloquent query construction

的方法where

(https://laravel.com/api/5.4/illuminate/database/eloquent/builder.html#method_where(

使用Collection

的方法where

(https://laravel.com/api/5.4/illuminate/support/collection.html#method_where(。

实际上,您正在尝试使用Collection,就像是query builder一样,因为您也尝试执行get

您将Eloquent whereCollection where混合。

将代码更改为: -

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

您可以像这样使用

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

相关内容

最新更新