Laravel 9 - How to Get Last Executed Query
Hello Artisans, today I'll talk about how to get the last executed query log. Which will also help us to see which raw SQL query is being executed. We'll also print the executed query through our Eloquent or any type of query which is being executed. We'll see 3 types of examples in our today's article.
Example 1
First, we'll see the below toSql() method.
It'll produce the below output
Example 2
Now we'll use the enableQueryLog() and getQueryLog() method of DB class. Like below
It'll produce the output in an nested array.
Example 3
Here we'll repeat the step2, rather we'll produce the nested array in a flat array. Or in other words, if we want to say, we'll flatten the array of step2 results. So let's see the below code.
Here what we'll do is that we'll only dd() the last array of getQueryLog() array. While we'll dd() the whole getQueryLog() array in step2. So, it'll produce the below output.
That's it for today. Hope you'll enjoy this tutorial and it'll help you to debug the whole project and help to improve the performance. Thanks for reading :)