我正在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();
我的语法也正确,我不知道这个问题会在哪里发生。帮助我解决这个问题。
答案 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 studio
与Eloquent where
混合。
将您的代码更改为: -
Collection where
答案 2 :(得分:1)
你可以这样使用
$tasks->investorCompletedCount = $tasks
->where(['task_id' => 'Investor Targets','status' => 'Closed'])
->get()
->count();