1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravelでクエリの実行回数を知る方法

Last updated at Posted at 2023-10-25

DBファサードをuseする

use Illuminate\Support\Facades\DB;

確認したいクエリが呼ばれている箇所をDB::enableQueryLog();dd(DB::getQueryLog());で囲む

DB::enableQueryLog();
$this->buildResult($result);
dd(DB::getQueryLog());

こんな感じになる

array:5 [▼ // app/XXX/XXXX.php:61
  0 => array:3 [▶
    "query" => "select なんちゃら...

query が呼ばれている回数がクエリ発行数。
配列の中身の回数分呼ばれるので、arrayの数がクエリ発行数。
この場合は5回呼ばれている。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?