带有日期范围和orWhere的Laravel查询返回错误结果



需要帮助制定雄辩的查询,我有这个查询,使用orWhere,但是当我想在查询中添加日期范围时,我无法得到正确的结果。

comment table

<表类> id 内容 user_id post_id created_at is_owner tbody><<tr>1测试112022 - 07 - 09年t04:28:50假2测试1222022 - 07 - 10 t04:28:50对3测试2232022 - 07 - 11 t04:28:50对4测试3222022 - 07 - 11 t04:28:50假5测试4332022 - 07 - 12 t04:28:50对6测试5222022 - 07 - 14 t04:28:50假7测试6422022 - 07 - 14 t04:28:50假8测试7512022 - 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;
});
}

相关内容

  • 没有找到相关文章

最新更新