簡體   English   中英

Laravel-針對數據庫中日期范圍的PHP / MySQL日期范圍過濾器

[英]Laravel - PHP/MySQL date range filter against date range in database

我正在使用Laravel開發應用程序,並且試圖從數據庫中過濾記錄。 這是標准:

在數據庫中,我有2個日期列[excluded_period_start][excluded_period_end] 兩列都具有日期數據類型。

現在,我在[start_date][end_date]表單中有2個字段。

我想獲取除數據庫中存儲的期間以外的所有記錄。 我使用的代碼是:

      $hotels = Hotel::whereHas('location' , function($query) use($searchOptions){


            if(trim($searchOptions['location']) != ''){
              $query->where('location_title', $searchOptions['location']);
            }

          })
          ->where('excluded_period_start', '<', $start)
          ->where('excluded_period_end', '>', $end)
          ->where('active', 1)
          ->take(10)
          ->paginate(10);

但是,這只會給我結果介於數據庫中存儲的范圍之間,但我希望結果超出該范圍。

我已經嘗試了很多類似->whereBetween()但是沒有一個起作用。

任何幫助,將不勝感激。

您最初的exclude_period不能在開始和結束之間:

          ->whereNotBetween('excluded_period_start', [$start, $end])
          ->whereNotBetween('excluded_period_end', [$start, $end])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM