14
17

More than 5 years have passed since last update.

【Laravel】LaravelでDISTINCTを使用する、かつ実行されているSQLを確認してみる

Last updated at Posted at 2018-10-05

LaravelでDISTINCTを使用する

例としてあるユーザーのチームIDの重複を消したい場合

self::where('user_id', $user_id)->distinct()->select('team_id')->get();

self ・・・ 自身のModelを指す(テーブル名はtablesとする)

SQLはどうなってるの

以下のようにtoSql()関数で実行されるSQLを吐き出すことができる

var_dump(self::where('user_id', $user_id)->distinct()->select('team_id')->toSql());

var_dump出力

select distinct `team_id` from `tables` where `user_id` = ?

おわり

  • SQL見ることなんてあんまりないけど、どんなSQL実行されてるのかきになるよね

参考

14
17
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
14
17