<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wiby Mahan Faqih on Medium]]></title>
        <description><![CDATA[Stories by Wiby Mahan Faqih on Medium]]></description>
        <link>https://medium.com/@wibymf?source=rss-69365a380981------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*iU9sgEDfQGx7VEuS</url>
            <title>Stories by Wiby Mahan Faqih on Medium</title>
            <link>https://medium.com/@wibymf?source=rss-69365a380981------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:25:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wibymf/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Stop Using whereDate() Method on Indexed DateTime Field in Laravel]]></title>
            <link>https://medium.com/mamitech/avoid-using-wheredate-method-on-indexed-field-in-laravel-72eca54c4d68?source=rss-69365a380981------2</link>
            <guid isPermaLink="false">https://medium.com/p/72eca54c4d68</guid>
            <category><![CDATA[laravel-query-builder]]></category>
            <category><![CDATA[mamikos]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[database-index]]></category>
            <dc:creator><![CDATA[Wiby Mahan Faqih]]></dc:creator>
            <pubDate>Mon, 27 Jun 2022 04:42:50 GMT</pubDate>
            <atom:updated>2022-06-27T09:11:59.691Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose we have users table with index in created_at field, and we want to retrieve all users that registered in a specific date. We could use query builder with <a href="https://laravel.com/docs/5.8/queries#where-clauses">whereDate(</a>) method like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AHQ8o1gC1TSD_HZBVqJFwQ.png" /></figure><p>And the query would be something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q90eQn3sAXODcghIJCT7tg.png" /></figure><p>The query cost 1.51 s because sql DATE() function converts selected field into datestring which will precludes the use of an index. So, it will iterate over all records.</p><p>This is the explain query:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*abyOvTeoxEf3touwe_QIJw.png" /></figure><h4>Solution</h4><p>Instead of using whereDate(), we could use where() method like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_JzEaeWsBKYmOij2Dho93w.png" /></figure><p>The query would be something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_nKD0T_Yp5VIV_vhipP3Lg.png" /></figure><p>The query cost reduced to less than 10 ms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gG7hLXU11KgAIJg-zRFy8w.png" /></figure><p>We can look at this explain query that it is using index.</p><h4>Conclusion</h4><p>Avoid using whereDate() method on indexed date field because it will convert the field into datestring which will precludes the use of an index.</p><p>This also applies to whereDay(), whereMonth(), whereYear() and whereTime() methods.</p><h4>Note</h4><p>There is a closed issue about this in Laravel repository. It does make sense that whereDate() method is translated into sql DATE(), but <a href="https://laravel.com/docs/5.8/queries#where-clauses">the documentation</a> should at least note about this behaviour and its side effect, which will prevent the use of indexes.</p><p><a href="https://github.com/laravel/framework/issues/41522">Builder::whereDate() prevents the use of indexes · Issue #41522 · laravel/framework</a></p><p><em>P.S.: If you want to study Laravel together and find more secrets, </em><a href="https://mamikos.com/career"><em>Join Us</em></a><em>!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72eca54c4d68" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mamitech/avoid-using-wheredate-method-on-indexed-field-in-laravel-72eca54c4d68">Stop Using whereDate() Method on Indexed DateTime Field in Laravel</a> was originally published in <a href="https://medium.com/mamitech">mamitech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>