LoginSignup
3
0

laravel クエリビルダ whereBetween

Last updated at Posted at 2022-11-01

概要

  • whereBetweenの使い方と注意点を簡単にまとめる。

使い方

  • 下記のように使う。(公式ドキュメントの内容そのまま拝借)

    $users = DB::table('users')
       ->whereBetween('votes', [1, 100])
       ->get();
    
  • 上記の処理はusersテーブルのvotesカラムの値が1~100の間のレコードをすべて取得する。ヒットした情報はCollectionで返される。

  • 上記の処理をあえてwhereで書き換えると下記の様になる。

    $users = DB::table('users')
       ->where('votes', '>', 1)
       ->where('votes', '<', 100)
       ->get();
    

注意点

  • whereBetweenは><で比較される。すなわちwhereBetweenで指定した上限、下限の値を含まない。

  • もし上限、下限の値を含めて比較してほしい場合(で比較したい場合)はwhereを使って下記の様に表現する他ないかもしれない。

    $users = DB::table('users')
       ->where('votes', '>=', 1)
       ->where('votes', '<=', 100)
       ->get();
    

参考文献

3
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
3
0