Welcome to the incredibly popular Easy Laravel 5 companion blog. To celebrate the new edition's release (updated for Laravel 5.5!) use the discount code easteregg to receive 20% off the book or book/video package! » Buy the book
Laravel's Eloquent and Query Builder features offer powerful solutions for interacting with your project database. Sometimes though you can be left second-guessing yourself when constructing queries using Laravel's syntax. Fortunately it's easy to view the resulting SQL query. Consider the following Eloquent statement:
$events = Event::where('city', 'Miami')->get();
This would translate to the following SQL:
SELECT * FROM events WHERE city = 'Miami';
You can log the resulting query to your project's log file using the
This will output the SQL as it looks prior to binding the parameters, but even so it gets the job done:
SELECT * FROM `events` WHERE `city` = ?
If you're using Query Builder, the same
toSql method can be used to output the query:
You can avoid the need to manually do any of this by instead installing the fantastic Laravel Debugbar. It will present a list of all queries executed in conjunction with loading a page; all you have to do is click on its
Queries tab to view the queries. Check out the project README via the above link for more details.