<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: John Roux</title>
    <description>The latest articles on DEV Community by John Roux (@johnroux).</description>
    <link>https://dev.to/johnroux</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F402771%2F2761ce04-c4e8-4f3a-885b-714a68226f89.jpeg</url>
      <title>DEV Community: John Roux</title>
      <link>https://dev.to/johnroux</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnroux"/>
    <language>en</language>
    <item>
      <title>Laravel SQL Debugging</title>
      <dc:creator>John Roux</dc:creator>
      <pubDate>Fri, 05 Jun 2020 18:12:33 +0000</pubDate>
      <link>https://dev.to/johnroux/laravel-sql-debugging-26dg</link>
      <guid>https://dev.to/johnroux/laravel-sql-debugging-26dg</guid>
      <description>&lt;p&gt;If you're not using &lt;a href="https://github.com/barryvdh/laravel-debugbar"&gt;Laravel DebugBar&lt;/a&gt; you should really think about it.&lt;/p&gt;

&lt;p&gt;Improving the performance of your Laravel app is a lot more than just removing N+1 db problems. DebugBar shows you timings and also model and memory usage.&lt;/p&gt;

&lt;p&gt;But if you're not keen, or you can't use DebugBar for some reason (It can be a mission getting it working with API's etc) then a nice solution is to have a little Database Listener which logs all the SQL Queries into your laravel log.&lt;/p&gt;

&lt;p&gt;This can also be very useful for debugging problems on staging or live environments as you can turn it on/off easily based on the env/config&lt;/p&gt;

&lt;p&gt;How it works:&lt;/p&gt;

&lt;p&gt;Add this to your AppServiceProvider's boot method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; if (env("SQL_DEBUG_LOG"))
    {
        DB::listen(function ($query) {
            Log::debug("DB: " . $query-&amp;gt;sql . "[".  implode(",",$query-&amp;gt;bindings). "]");
        });
    }

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you're using it in production I'd also recommend pulling that env into config and using that, as then you can (and should) cache the config&lt;/p&gt;

&lt;p&gt;Another issue with it that I've found is if the sql call itself fails, and throws an exception, it's not logged as DB::listen only logs after a successful call and the exception breaks out before it get's back to the listen&lt;/p&gt;

&lt;p&gt;If you're having laravel performance issues, another great resource to look at is the eloquent course from Jonathan Reinink&lt;br&gt;
&lt;a href="https://eloquent-course.reinink.ca/"&gt;https://eloquent-course.reinink.ca/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>performance</category>
      <category>sql</category>
      <category>debugbar</category>
    </item>
  </channel>
</rss>
