Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@meso_

[Laravel]直近のクエリ文を出力する

More than 3 years have passed since last update.

クエリ文を文字列として出力する

$sql = User::orderBy('id', 'desc')->toSql();
var_dump($sql);
  • 出力結果
 'select * from `users` where `users`.`deleted_at` is null order by `id` desc' (length=75)

もっと詳細を出力する場合

DB::enableQueryLog();
$users = User::orderBy('id', 'desc')->paginate(30);
dd(DB::getQueryLog());
  • 出力結果
array:2 [▼
  0 => array:3 ["query" => "select count(*) as aggregate from `users` where `users`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.38
  ]
  1 => array:3 ["query" => "select * from `users` where `users`.`deleted_at` is null order by `id` desc limit 20 offset 0"
    "bindings" => []
    "time" => 0.56
  ]
]
3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
meso_

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?