【发布时间】:2017-09-27 14:09:48
【问题描述】:
我正在尝试在 Laravel 中实现此查询
"SELECT * FROM jobs
WHERE status = 1 AND
taskerCount = (SELECT count(*) FROM jobRequests WHERE status = 1 AND job_id =
jobs.id)"
这是我尝试过的;
Auth::user()->jobs
->where('status', 1)
->where('taskerCount', function ($q) {
$q->where('status', 1)
->where('job_id', $q->id)->count();
});
但我得到了错误:
Closure 类的对象无法转换为 int。
【问题讨论】:
-
你不能使用 ->count 构建器,你只能在语句执行后使用它。您需要使用原始 sql 来选择它作为构建器返回的内容。
-
即使我将其更改为 $q->select(DB::raw('count(*)')) ->where('status', 1) ->where('job_id' , $q->id);我仍然得到同样的错误。而且我不确定 $q->id 返回什么。
-
查看我的答案以获取我过去使用过的示例
标签: php mysql laravel orm eloquent