gpt4 book ai didi

laravel - laravel搜索日期不在查询之间

转载 作者:行者123 更新时间:2023-12-02 02:44:06 27 4
gpt4 key购买 nike

我必须搜索具有给定日期和时间(早上和晚上)的数据。如何通过laravel查询从数据中找到可用日期?
如果我搜索的日期是 2019-06-10 2019-06-18 。它应该只返回ID 3和4。在此查询中,它返回ID 1、3和4。没有在两个日期之间进行检查的查询有什么问题?我在数据库中有日期类型列作为开始和结束日期。
我想检查两个条件

  • 给定日期应不等于date_from和date_to
  • 给定的日期不在这些日期之间

    两个条件都在同一查询中。希望你能理解。
    id  event_id   date_from    date_to   

    1 1 2019-06-08 2019-06-12
    2 1 2019-06-14 2019-06-16
    3 2 2019-06-20 2019-06-25
    4 3 2019-07-26 2019-07-27

    $getEvents = Booking::where('category_id', '=', $categoryID)
    ->where('date_from', '!=', $startDate)
    ->where('date_to', '!=', $endDate)
    ->orWhere('date_from', '<', $startDate)
    ->orWhere('date_to', '>', $endDate)
    ->whereNotBetween('date_from', [$startDate,$endDate])
    ->whereNotBetween('date_to', [$startDate,$endDate])
    ->get();

  • 最佳答案

    假设$endDate总是大于(或等于)$startDate

    $startDate = "2019-06-10";
    $endDate = "2019-06-18";


  • 给定的日期不在这些日期之间


            ->whereDate('date_to', '<', $startDate)
    ->whereDate('date_from', '>', $endDate)

    如果给定的$startDate大于db 'date_to',则$startDate不在'date_to''date_from'之间。将检索'date_to'小于"2019-06-10"的记录。

    当给定的'date_from'大于$endDate时,$endDate不在'date_to''date_from'之间。将检索'date_from'大于"2019-06-18"的记录。


  • 给定的日期应不等于date_from和date_to


  • 这是不需要的,因为在点2的查询中,我们使用>和<,这将不考虑等于。

        //->where('date_from', '<>', $startDate)->where('date_from', '<>', $endDate)
    //->where('date_to', '<>', $startDate)->where('date_to', '<>', $endDate)


    只是:

    $getEvents = Booking::where('category_id', '=', $categoryID)
    ->whereDate('date_to', '<', $startDate)
    ->whereDate('date_from', '>', $endDate)
    ->get();

    关于laravel - laravel搜索日期不在查询之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56891743/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com