我有兩個(gè)模型用戶和任務(wù),它們之間存在多對(duì)多關(guān)系,任務(wù)與自身作為父子關(guān)系具有自嵌套關(guān)系,現(xiàn)在我想要獲取當(dāng)前空閑的用戶,即沒有分配任務(wù)或分配的任務(wù)狀態(tài)為已完成或已關(guān)閉,下面是我編寫的代碼和它的 toSql,但我仍然讓用戶的任務(wù)具有其他狀態(tài),例如 -進(jìn)展和審查不足$freeEmployees = User::where('role', 'employee')->where(function ($q) { $q->doesntHave('task') ->orWhereHas('task', function ($q1) { $q1->where('status', config('taskstatus.completed')) ->orWhere('status', config('taskstatus.closed')); }); })->get()->take($limit);SELECT *FROM `users`WHERE `role` = 'employee' AND( NOT EXISTS( SELECT * FROM `task_checklist` INNER JOIN `user_task` ON `task_checklist`.`id` = `user_task`.`task_id` WHERE `users`.`id` = `user_task`.`user_id` AND `task_checklist`.`archived_at` IS NULL ) OR EXISTS( SELECT * FROM `task_checklist` INNER JOIN `user_task` ON `task_checklist`.`id` = `user_task`.`task_id` WHERE `users`.`id` = `user_task`.`user_id` AND( `status` = 'Completed' OR `status` = 'Closed') AND `task_checklist`.`archived_at` IS NULL) )ORDER BY `updated_at`DESC
1 回答

千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
為什么不以另一種方式做......
在他們沒有未完成的任務(wù)時(shí)讓我的員工...
$freeEmployees = User::where('role', 'employee')->whereDoesntHave('tasks', function ($q) {
$q1->where('status', config('taskstatus.running'))
->orWhere('status', config('taskstatus.uncompleted'));
});
})->get()->take($limit);
- 1 回答
- 0 關(guān)注
- 142 瀏覽
添加回答
舉報(bào)
0/150
提交
取消