我正在在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 where
与Collection 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();