1
3

More than 3 years have passed since last update.

Laravelのクエリビルダ(Eloquent)のまとめ

Posted at

LaravelでのSQLの書き方がごっちゃになってきたので、まとめる。

Raw

$users = DB::select('select * from users')  

insert, update, delete でも同様

クエリビルダ

$email = DB::table('email')
   ->join('telephone',  'email.id', '=', 'telephone.id') //combining methods
   ->where('vip', true) //constraing methods
   ->orderBy('last_name','desc') //modifiyng methods
   ->get() //ending methods

combining methods

  • join
  • union

constraining methods

  • select
  • where
  • orWhere
  • whereBetween
  • whereIn
  • whereNull
  • whereExists
  • distinct

modyfing methods

  • orderBy
  • groupBy and having
  • skip and take
  • latest/oldest
  • inRandomOrder

conditional methods

  • when
  • unless

ending methods

  • get
  • first
  • find
  • value
  • count
  • min/max
  • sum/avg
  • all(ただしこれは、Eloquentのときだけ)

まとめ

特にgetを忘れてエラーを出しやすい。
findやallのときはいらないが、それ以外の時は基本いるものと考えたほうが良い。

1
3
0

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