Nit*_*mar 1 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);
Run Code Online (Sandbox Code Playgroud)
我得到了以下错误;
缺少Illuminate\Support\Collection :: where()的参数2
在线
$tasks->investorCompletedCount = $tasks->where([
['task_id', '=', 'Investor Targets'],
['status', '=', 'Closed'],
])->get()->count();
Run Code Online (Sandbox Code Playgroud)
我的语法也正确,我不知道这个问题会在哪里发生.帮助我解决这个问题.
这段代码
$tasks->investorCompletedCount = $tasks->where([
['task_id', '=', 'Investor Targets'],
['status', '=', 'Closed'],
])->get()->count();
Run Code Online (Sandbox Code Playgroud)
需要重写为
$tasks->investorCompletedCount = $tasks->where('task_id', 'Investor Targets')
->where('status', 'Closed')->count();
Run Code Online (Sandbox Code Playgroud)
问题是你混淆了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).
实际上你正在尝试使用a Collection,就好像它是一个query builder,因为你也尝试执行一个get.