
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
In this article, I will show you 2 ways to get the executed queries in plain SQL so that you can debug faster in Laravel.
We will cover
toSql
To Get QueryBasic knowledge of Laravel queries running.
toSql
To Get QueryFirst lets see how to use to toSql
function to get the eloquent query in plain SQL format
Think your running the following query and want to get the plain sql
$posts = Post::with(['user', 'tags'])
->published()
->orderByDesc('published_at')
->get();
To get the plain SQL format of the above query use toSql()
in the end instead of get()
as shown below
$posts = Post::with(['user', 'tags'])
->published()
->orderByDesc('published_at')
->toSql();
echo '<pre>';
print_r($posts);
The output of the above eloquent query will be as follows
select * from `posts` where `published_at` <= ? and `posts`.`deleted_at` is null order by `published_at` desc
By using toSql()
it will only show the current query executing and does not show the query it ran for with(['user', 'tags'])
and doesn't show what parameters its using. And in a function if your running 4 to 5 query then you have to manually check each and every query.
DB::enableQueryLog();
& DB::getQueryLog()
By using the above functions we can resolve the problems faced with toSql()
and other problems as explained above
/** Enable Query To Log All The Query Benith It */
\DB::enableQueryLog();
$posts = Post::with(['user', 'tags'])
->published()
->orderByDesc('published_at')
->get();
echo '<pre>';
/** With the help of the following function will tell to display all the query logged and stop logging further */
print_r(\DB::getQueryLog());
DB::enableQueryLog()
- Use this line of code where you want to start logging the queries
DB::getQueryLog()
- Use this line of code where you want to stop the logging of query and dump the output of the logs to the screen for debugging.
By running the above eloquent query it will show all the 3 queries ran to fetch the post details as below
Array
(
[0] => Array
(
[query] => select * from `posts` where `published_at` <= ? and `posts`.`deleted_at` is null order by `published_at` desc
[bindings] => Array
(
[0] => 2020-06-08 16:10:48
)
[time] => 22.08
)
[1] => Array
(
[query] => select * from `users` where `users`.`id` in (1)
[bindings] => Array
(
)
[time] => 3.78
)
[2] => Array
(
[query] => select `tags`.*, `posts_tags`.`post_id` as `pivot_post_id`, `posts_tags`.`tag_id` as `pivot_tag_id` from `tags` inner join `posts_tags` on `tags`.`id` = `posts_tags`.`tag_id` where `posts_tags`.`post_id` in (?, ?, ?, ?) and `tags`.`deleted_at` is null
[bindings] => Array
(
[0] => 71f0495d-6dc3-49c8-bf40-353c2be37be3
[1] => 91bdcb4d-e43c-4c5b-856c-978d22f139bc
[2] => 9a2a13ac-f5a5-406e-9a9b-7ab80aa1a6fb
[3] => d49c93b5-75b0-41ab-ac79-4324bc116b96
)
[time] => 8.29
)
)
Hope this was helpful. Kindly share with your friends :)
Google reCAPTCHA Integration In PHP Laravel Forms
Send Email In PHP With PHPMailer
Plain PHP Resumable Large File Uploads In Chunks Using FlowJs
Create / Save / Download PDF From Blade Template In PHP Laravel
Laravel Clear Cache Of Route, View, Config Command
Create A Composer Package? Test It Locally And Add To Packagist Repository
NGINX Security Best Practices & Optimization
composer.json v/s composer.lock
Generate Sitemap in PHP Laravel
PHP extension ext-intl * is missing
URL Redirects From Called Functions In Laravel