Laravel offers several ways to check the performance of an eloquent query.
1] Using DB query log
You can log the SQL queries and get the performance of individual queries using DB::enableQueryLog()
within your application and getting the log using DB::getQueryLog()
.
DB::enableQueryLog();
$post = Post::with(['author'])->first();
dd(DB::getQueryLog());
The output gives the list of SQL queries performed and their time taken in milliseconds.
2] Using Benchmark class
Starting from Laravel 9.32 you can use the Benchmark class like so:
Benchmark::dd(fn () => Post::with(['author'])->first(), iterations: 10);
The Benchmark
class allows you to perform multiple iterations and return an average value.
3] Using Laravel Debugbar
Laravel DebugBar allows you to view performance of several SQL queries in the browser on the page you are executing the line of code.