Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

期間検索で矛盾した期間が入力された時の対処してみた

Last updated at Posted at 2024-11-29

期間検索でありえない入力された時の対処してみた

ニュースとか通知で期間を指定して検索かけるときありますよね?
そのとき終了日時が開始日時以前で検索かけても絶対にヒットはしないですが、現実あり得ないのでそのまま処理が走らないようにしてみました。
かなり簡単なので記事にしやすいことと、使う頻度高そうと思ったので書いてみます。

日時の入れ替え

実際に実装したものは終了日時が開始日時以前の入力になり、検索が押された場合開始と終了日時を入れ替えて検索をかける処理です。
聞いたとき難しそうと思っていたのですが、かなり簡単でした!

コード例

public function search(Request $request)
{
    $searchDate = $request->filled('search_date') ? $request->search_date : now()->toDateString();

    $news = News::where(function ($query) use ($searchDate) {
        $query->whereNull('publish_start_date')
              ->orWhere('publish_start_date', '<=', $searchDate);
    })->where(function ($query) use ($searchDate) {
        $query->whereNull('publish_end_date')
              ->orWhere('publish_end_date', '>=', $searchDate);
    })->get();

    return view('news.index', compact('news', 'searchDate'));
}

この状態だと日時の入力形式さえ合っていればあり得ない期間検索でも処理自体は走ってしまいます。(ヒットはしないですが…)

ここで日時を入れ替える処理を入れることで開始日時以前が入力されても日時を入れ替えて適切な期間で検索することができます。

//日時の入れ替え処理
if($publish_end_date < $publish_start_date){
    $evacuation_date = $publish_end_date;
        $publish_end_date = $publish_start_date;
        $publish_start_date = $evacuation_date;
    }
    
$news = News::where(function ($query) use ($searchDate) {
        $query->whereNull('publish_start_date')
              ->orWhere('publish_start_date', '<=', $searchDate);
    })->where(function ($query) use ($searchDate) {
        $query->whereNull('publish_end_date')
              ->orWhere('publish_end_date', '>=', $searchDate);
    })->get();

コード説明

開始日時を別変数に移すー>終了日時を開始日時に移すー>別変数を終了日時に移す
これだけなんです!笑
ぶっちゃけなくてもいいのかも知れないですが、使う側からするとほんの少しありがたいと感じるかも知れないので機会があればぜひ実装してみてください!

変数名を変えればいいだけなので楽々使いまわせますよ!!

終わりに

初めて技術記事書いてみましたがまだまだ拙いところもたくさんありますがそこはご愛嬌ということでお願いします。

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
eda-inc
スマホアプリでできること全部。 私たち、株式会社イーディーエーは スマホアプリ専門のシステム開発会社です。

Comments

srj927483
@srj927483
//日時の入れ替え処理
if($publish_end_date < $publish_start_date){
   $evacuation_date = $publish_end_date;
       $publish_end_date = $publish_start_date;
       $publish_start_date = $evacuation_date;
   }

開始日時を別変数に移すー>終了日時を開始日時に移すー>別変数を終了日時に移す
これだけなんです!笑

二つの変数の値の交換はブラケットを使用する方法がよく使われますね。

if ($publish_end_date < $publish_start_date) {
  [$publish_end_date, $publish_start_date] = [$publish_start_date, $publish_end_date];
}
0

Let's comment your feelings that are more than good

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address