我試圖通過檢查開始日期和結束日期范圍來獲取沒有作品的用戶。
$production = Production::find($id);
$users = User::whereDoesntHave('productions', function ($query) {
return $query
->where(function ($q) {
return $q->whereBetween('start_at', [$production->start_at, $production->end_at])
->whereBetween('end_at', [$production->start_at, $production->end_at]);
});
})->get();
//User Production relation
public function productions()
{
return $this->belongsToMany(Production::class)
->withPivot(['created_at', 'updated_at']);
}
這將回傳在當前生產開始日期和結束日期內已在其他生產中分配的用戶,我只想獲取在當前日期范圍內沒有生產分配的用戶。
uj5u.com熱心網友回復:
如果我理解正確,您在$production變數中有一個 Production 模型實體。您希望所有沒有生產與$production實體時間范圍重疊的用戶?
您當前的代碼(從您的問題中粘貼):
$production = Production::find($id);
$users = User::whereDoesntHave('productions', function ($query) {
return $query
->where(function ($q) {
return $q->whereBetween('start_at', [$production->start_at, $production->end_at])
->whereBetween('end_at', [$production->start_at, $production->end_at]);
});
})->get();
你應該做什么:
$production = Production::find($id);
$users = User::whereDoesntHave('productions', function ($query) use($production) {
return $query->whereBetween('start_at', [$production->start_at, $production->end_at])
->orWhereBetween('end_at', [$production->start_at, $production->end_at]);
});
})->get();
我認為這里的關鍵是在結果中包含開始和結束范圍,orWhereBetween而不是whereBetween. 并且不要忘記$production在閉包中使用變數;-)
如果這不是您想要做的,請發表評論以更準確地解釋您想要做什么
uj5u.com熱心網友回復:
您將日期作為引數傳遞,但您從未真正使用過它們,它應該是這樣的:
$users = User::whereDoesntHave('productions', function ($query) use ($startDate, $endDate) {
return $query->where(function ($q) use ($startDate, $endDate) {
return $q->whereBetween('start_at', [$startDate,$endDate])
->orWhereBetween('end_at', [$startDate, $endDate]);
});
})
->get();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/351650.html
上一篇:Vue-以相同的形式編輯和創建
下一篇:如何在貝寶付款中完成待處理的交易
