需要帮助制定雄辩的查询,我有这个查询,使用orWhere,但是当我想在查询中添加日期范围时,我无法得到正确的结果。
comment table
<表类>
id
内容
user_id
post_id
created_at
is_owner
tbody><<tr>1 测试 1 1 2022 - 07 - 09年t04:28:50 假 2测试1 2 2 2022 - 07 - 10 t04:28:50 对 3 测试2 2 3 2022 - 07 - 11 t04:28:50 对 4测试3 2 2 2022 - 07 - 11 t04:28:50 假 5测试4 3 3 2022 - 07 - 12 t04:28:50 对 6测试5 2 2 2022 - 07 - 14 t04:28:50 假 7测试6 4 2 2022 - 07 - 14 t04:28:50 假 8测试7 5 1 2022 - 07 - 15 t04:28:50 假 表类>
我错过了一件重要的事情。您需要为whereDate方法提供一个数组:
// whereDate need a query as first parameter
// i think the method is usually used in closures.
$comment->whereDate($comment, ['created_at', '>=', '2022-07-13'])
$comment->whereDate($comment, ['created_at', '<=', '2022-07-15']);
usewhereBetween试试这个查询
$from ="2022-07-09";
$to = "2022-07-12";
$postIds = [1,2];
$comment = Comment::where(function ($query) use ($postIds, $userId,$from, $to) {
$query->whereIn('post_id', $postIds)
->where('user_id', $userId)
->whereBetween('created_at', [$from, $to]);
}
)->get();
if ($isCommentOwner) {
$comment = $comment->filter(function ($item){
return $item->is_owner == true;
});
}