<?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: Ryan</title>
    <description>The latest articles on DEV Community by Ryan (@ryancco).</description>
    <link>https://dev.to/ryancco</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%2F226662%2Fd7994b12-3eb1-47df-9965-c325bb1e0f6a.png</url>
      <title>DEV Community: Ryan</title>
      <link>https://dev.to/ryancco</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ryancco"/>
    <language>en</language>
    <item>
      <title>Cleaning up queries with dot notation</title>
      <dc:creator>Ryan</dc:creator>
      <pubDate>Wed, 16 Jun 2021 01:30:57 +0000</pubDate>
      <link>https://dev.to/ryancco/cleaning-up-queries-with-dot-notation-4nh2</link>
      <guid>https://dev.to/ryancco/cleaning-up-queries-with-dot-notation-4nh2</guid>
      <description>&lt;p&gt;&lt;em&gt;🔗 This article was originally posted, with additional formatting, on my personal blog at &lt;a href="https://ryanc.co/blog/cleaning-up-queries-using-dot-notation"&gt;https://ryanc.co/blog/cleaning-up-queries-using-dot-notation&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One of the many conventions found throughout the Laravel framework is dot notation. Dot notation is the use of dots &lt;code&gt;.&lt;/code&gt; in strings to represent depth. In many instances, it allows for writing cleaner and more clear code. Take for example it's use case of accessing nested elements in an array using the &lt;code&gt;Arr&lt;/code&gt; helper&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Arr&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'user.profile.billing_details.address'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This not only looks nicer but it also takes care of handling undefined index errors and this method specifically has the added benefit of providing a default value as a third parameter. Use cases similar to this can be found in most &lt;code&gt;Arr&lt;/code&gt; methods, most &lt;code&gt;Collection&lt;/code&gt; methods, when validating arrays, and in most &lt;code&gt;array_*&lt;/code&gt; helper methods (for those of you on legacy versions of Laravel) to name a few.&lt;/p&gt;

&lt;h2&gt;
  
  
  Eloquent &amp;amp; dot notation
&lt;/h2&gt;

&lt;p&gt;Support for dot notation can can also be found throughout Laravel when interacting with Eloquent relations.&lt;/p&gt;

&lt;h3&gt;
  
  
  whereHas()
&lt;/h3&gt;

&lt;p&gt;For example, say you want to query a &lt;code&gt;User&lt;/code&gt; model for all records that have an associated &lt;code&gt;Invoice&lt;/code&gt; record which has an associated &lt;code&gt;Payment&lt;/code&gt; record. While this can be done a handful of ways, one way would be to use the &lt;code&gt;whereHas()&lt;/code&gt; Eloquent Builder method, like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereHas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'invoices'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$invoices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$invoices&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;whereHas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'payment'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome! Task achieved. But consider the following alternative using dot notation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereHas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'invoices.payment'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the contrived example may only save you a few lines/characters, that is still characters saved, at no expense to the readability of the code — if anything, it's improved. This usage of dot notation can be used to clean up complex nested relationships that would otherwise result in deep chains of single-depth &lt;code&gt;whereHas()&lt;/code&gt; methods. Most importantly, &lt;strong&gt;the resulting query is &lt;em&gt;exactly&lt;/em&gt; the same between the two examples&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="k"&gt;exists&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;`invoices`&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`id`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;`invoices`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`user_id`&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="k"&gt;exists&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;`payments`&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="nv"&gt;`invoices`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`payment_id`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;`payments`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`id`&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;`payments`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`deleted_at`&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;`invoices`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`deleted_at`&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`deleted_at`&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As an added bonus, you still have the flexibility of breaking away from dot notation at any level of depth and passing a closure to the second parameter of &lt;code&gt;whereHas()&lt;/code&gt; to add query constraints and even continue with dot notation as part of your constraints. Still, the resulting query will be exactly the same as it's single-depth, chained &lt;code&gt;whereHas()&lt;/code&gt; counterpart.&lt;/p&gt;

&lt;h3&gt;
  
  
  with()
&lt;/h3&gt;

&lt;p&gt;One usage of dot notation that I find more powerful than in &lt;code&gt;whereHas()&lt;/code&gt; methods is in &lt;code&gt;with()&lt;/code&gt; methods to eager load related models. Though, I often find myself questioning its inner-workings.&lt;/p&gt;

&lt;p&gt;Using our previous example, say you want to not only fetch users with paid invoices, but you also want to fetch them with their related &lt;code&gt;Invoice&lt;/code&gt; and &lt;code&gt;Payment&lt;/code&gt; records eagerly loaded. One way to achieve this would be using single-depth, chained &lt;code&gt;with()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;As you've probably guessed, you can use dot notation for that too! So I'll save you the time and get to the interesting part: say you only want to load these related models but only with select attributes to limit memory usage. Using the &lt;code&gt;with()&lt;/code&gt; method without dot notation would look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'invoices'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$invoices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$invoices&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'amount'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'payment_id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'payment'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$payment&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'paid_at'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sprinkle on some dot notation magic and...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'invoices:id,amount,payment_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'invoices.payment:id,paid_at'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A clean, considerably easier to parse one-line equivalent. Clearly, this is more than dot notation alone and, as you can see, this additional functionality does not rely on dot notation. But, when paired, this duo can clean up otherwise long, deeply nested blocks of code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ℹ️ One thing to be mindful of when writing queries that select specific columns in general is to be sure to select &lt;code&gt;id&lt;/code&gt; and foreign key columns relating to any models you wish to load.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But... what's actually going on here? Is performance impacted, for each level of nesting are we re-querying the parent models? The most important thing to note is that &lt;strong&gt;both approaches result in the exact same query&lt;/strong&gt;. Meaning there are no redundant queries being made or any other performance implications one way or the other.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="nv"&gt;`users`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`deleted_at`&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="nv"&gt;`id`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;`amount`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;`payment_id`&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;`invoices`&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="nv"&gt;`invoices`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`user_id`&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;`invoices`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`deleted_at`&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="nv"&gt;`id`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;`paid_at`&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nv"&gt;`payments`&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="nv"&gt;`payments`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`id`&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;`payments`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;`deleted_at`&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;Safe to say, where it makes sense to, dot notation is a safe and viable way to clean up some of your Laravel queries without running the risk of any performance implications as it does not effect the resulting query in any way. Hopefully this has saved you at least a few minutes of tinkering trying to answer these questions on your own.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 Like the content? Feel free to let me know on &lt;a href="https://twitter.com/colson_ryan"&gt;Twitter&lt;/a&gt; where I share all of my articles, packages, and occasional opinions (&lt;em&gt;mostly&lt;/em&gt; technical).&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>eloquent</category>
    </item>
    <item>
      <title>Understanding Laravel's SerializesModels trait</title>
      <dc:creator>Ryan</dc:creator>
      <pubDate>Sat, 01 May 2021 17:51:26 +0000</pubDate>
      <link>https://dev.to/ryancco/understanding-laravel-s-serializesmodels-trait-a6e</link>
      <guid>https://dev.to/ryancco/understanding-laravel-s-serializesmodels-trait-a6e</guid>
      <description>&lt;p&gt;&lt;em&gt;🔗 This article was originally posted, with additional formatting, on my personal blog at &lt;a href="https://ryanc.co/blog/understanding-laravels-serializesmodels" rel="noopener noreferrer"&gt;https://ryanc.co/blog/understanding-laravels-serializesmodels&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;When dispatching an object onto the queue, behind the scenes Laravel is recursively serializing the object and all of its properties into a string representation that is then written to the queue. There it awaits a queue worker to retrieve it from the queue and unserialize it back into a PHP object (&lt;em&gt;Phew&lt;/em&gt;!). &lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;When complicated objects are serialized, their string representations can be atrociously long, taking up unnecessary resources both on the queue and application servers. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Because of this, Laravel offers a trait called &lt;code&gt;SerializesModels&lt;/code&gt; which, when added to an object, finds any properties of type &lt;code&gt;Model&lt;/code&gt; or &lt;code&gt;Eloquent\Collection&lt;/code&gt; during serialization and replaces them with a plain-old-PHP-object (&lt;a href="https://stackoverflow.com/a/41188144" rel="noopener noreferrer"&gt;POPO&lt;/a&gt;) known as a &lt;code&gt;ModelIdentifier&lt;/code&gt;. These identifier objects represent the original properties &lt;code&gt;Model&lt;/code&gt; type and ID, or IDs in the case of an &lt;code&gt;Eloquent\Collection&lt;/code&gt;, with a much smaller string representation when serialized. When these objects are unserialized, the &lt;code&gt;ModelIdentifier&lt;/code&gt;s are then replaced with the &lt;code&gt;Model&lt;/code&gt; or &lt;code&gt;Eloquent\Collection&lt;/code&gt; of &lt;code&gt;Model&lt;/code&gt;s that they temporarily represented.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ℹ️ Curious to know how the &lt;code&gt;SerializesModels&lt;/code&gt; trait is "replacing" these properties at runtime? Before jumping into the &lt;a href="https://github.com/laravel/framework/blob/8.x/src/Illuminate/Queue/SerializesModels.php" rel="noopener noreferrer"&gt;source code&lt;/a&gt;, you may want to read the &lt;a href="https://www.php.net/manual/en/intro.reflection.php" rel="noopener noreferrer"&gt;PHP docs&lt;/a&gt; for a quick primer on what the reflection API offers. For a more detailed explanation including examples of how Laravel uses reflection check out &lt;a href="https://culttt.com/2014/07/02/reflection-php/" rel="noopener noreferrer"&gt;this article&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Gotcha!
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;🗣 Because of the SerializesModels trait that the job is using, Eloquent models and their loaded relationships will be gracefully serialized and unserialized when the job is processing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While this quote from the docs sounds promising, it can be misleading. Here is an example of how a &lt;code&gt;Model&lt;/code&gt; would be represented when serialized.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FKwnEZsN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FKwnEZsN.png" alt="https://i.imgur.com/KwnEZsN.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the relation is serialized as &lt;code&gt;records&lt;/code&gt;, which is the name of a one-to-many relation between &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Record&lt;/code&gt; as it exists on the &lt;code&gt;User&lt;/code&gt; model. However, even though we're only selecting the &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;user_id&lt;/code&gt; columns and have a limit of 3 records to be returned with the &lt;code&gt;User&lt;/code&gt; model, there's no mention of which records, which properties, or how many in the serialized representation.&lt;/p&gt;

&lt;p&gt;Let's see what the query log looks like when we unserialize this object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F1oMrVXI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F1oMrVXI.png" alt="https://i.imgur.com/1oMrVXI.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wow!&lt;/em&gt; We're selecting &lt;em&gt;all&lt;/em&gt; &lt;code&gt;Record&lt;/code&gt; models, in their &lt;em&gt;entirety&lt;/em&gt;, associated to the &lt;code&gt;User&lt;/code&gt;! As you can probably imagine, this can cause unforeseen issues for the unexpecting artisan.&lt;/p&gt;

&lt;h2&gt;
  
  
  Workarounds
&lt;/h2&gt;

&lt;p&gt;Hope is not lost! There are workarounds without having to sacrifice resources on the queue, application, or database servers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unload unnecessary relations
&lt;/h3&gt;

&lt;p&gt;If you don't need the loaded relations to be re-loaded, you can simply call &lt;code&gt;withoutRelations()&lt;/code&gt; on your &lt;code&gt;Model&lt;/code&gt; before it's serialized.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FUZ4j14E.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FUZ4j14E.png" alt="https://i.imgur.com/UZ4j14E.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, there are no longer any relations that will be loaded when the &lt;code&gt;User&lt;/code&gt; model is unserialized.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make necessary relations their own property
&lt;/h3&gt;

&lt;p&gt;If the loaded relation is going to be required after the &lt;code&gt;Model&lt;/code&gt; is unserialized you can store the relation (which is an &lt;code&gt;Eloquent\Collection&lt;/code&gt;) as its own property on the object being serialized.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FVnJ1lKY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FVnJ1lKY.png" alt="https://i.imgur.com/VnJ1lKY.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the serialized representation of the &lt;code&gt;Eloquent\Collection&lt;/code&gt; specifies what the &lt;code&gt;Model&lt;/code&gt; type is and which IDs need to be retrieved from the database. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FWdPdrMl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FWdPdrMl.png" alt="https://i.imgur.com/WdPdrMl.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Much&lt;/em&gt; better! Now that our &lt;code&gt;Model&lt;/code&gt; and &lt;code&gt;Eloquent\Collection&lt;/code&gt; of &lt;code&gt;Record&lt;/code&gt;s have been hydrated, we can even set the relation back on the &lt;code&gt;Model&lt;/code&gt; by using the &lt;code&gt;setRelation&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;While it's recommended to use the &lt;code&gt;SerializesModels&lt;/code&gt; trait on any and all objects that will be queued (or otherwise serialized) it is crucial to be at least aware of its potential pitfalls and shortcomings as well as how to avoid running into them, if not understand how it all works under the hood.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 Like the content? Feel free to let me know on &lt;a href="https://twitter.com/colson_ryan" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; where I share all of my articles, packages, and occasional opinions (&lt;em&gt;mostly&lt;/em&gt; technical).&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Simple Scaling with Redis Lists</title>
      <dc:creator>Ryan</dc:creator>
      <pubDate>Thu, 29 Apr 2021 20:29:06 +0000</pubDate>
      <link>https://dev.to/ryancco/simple-scaling-with-redis-lists-37ld</link>
      <guid>https://dev.to/ryancco/simple-scaling-with-redis-lists-37ld</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally posted, with additional formatting, on my personal blog at &lt;a href="https://ryanc.co/blog/simple-scaling"&gt;https://ryanc.co/blog/simple-scaling&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Keeping It Simple
&lt;/h1&gt;

&lt;p&gt;Although simple and powerful, it's no secret that relational databases have their limitations. So what happens when simple doesn't cut it, and your relational database falls short? You might try throwing more servers at it, reaching for &lt;a href="https://stackoverflow.com/a/1163249"&gt;replication, or load balancing&lt;/a&gt; which would assist in scaling read-intensive operations. But how would you go about scaling &lt;em&gt;write-intensive&lt;/em&gt; operations? &lt;/p&gt;

&lt;p&gt;Take for example some of the problems I've faced working with clients in the Adtech space:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking impressions across an ad network&lt;/li&gt;
&lt;li&gt;Monitoring user behaviors and actions&lt;/li&gt;
&lt;li&gt;Tracking traffic to and from affiliate networks&lt;/li&gt;
&lt;li&gt;Capturing spikes of traffic resulting in periodic loads 10x higher than normal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these have required high data ingestion rates that couldn't be achieved the same way that you would remedy read-intensive bottlenecks. Rather than rearchitecting from the ground up, in each of these instances, I was able to keep it simple and lean in on tools that were already familiar and readily available. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Redis is an [...] in-memory data structure store, used as a database, cache, and message broker.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Although commonly used for caching the output of CPU intensive or time consuming tasks, Redis' use cases extend far beyond that. Redis supports various data types including hashes, lists, sets/sorted sets, and of course strings. Each offering the same speed and scalability that you've come to expect from the blazing fast, in-memory, highly scalable data store. &lt;/p&gt;

&lt;p&gt;We're going to look at an example of how to use &lt;a href="https://redis.io/topics/data-types#lists"&gt;Redis Lists&lt;/a&gt; for fast data ingestion to then process and persist the data to a relational database later on.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Redis Lists are simply lists of strings, sorted by insertion order.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lists support a maximum length of over 4 &lt;em&gt;billion&lt;/em&gt; elements and, as long as you're accessing those elements near the head and tail of the list, maintain &lt;em&gt;constant time&lt;/em&gt; insertion and deletion. &lt;em&gt;You can think of Lists as a feature-limited extension to your programming language's &lt;code&gt;array&lt;/code&gt; or &lt;code&gt;list&lt;/code&gt; data type.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because of this, Redis Lists are great for fast data ingestion. We can use Lists as a "&lt;em&gt;data channel&lt;/em&gt;" to push data into in real-time and then consume, process, and ingest the data into our relational database in more performant batches. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;All of the below pseudo code samples are in PHP and make references to components of the Laravel framework. However, the same methodologies can be applied to your language/framework of choice.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Filename: AdSpotController.php&lt;br&gt;
Description: Push data from an incoming request onto the end of the list as requests come in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Redis&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;rpush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'impressions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'spot_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;spot_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// more data points...&lt;/span&gt;
        &lt;span class="s1"&gt;'timestamp'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;]));&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Filename: ImpressionListConsumer.php&lt;/p&gt;

&lt;p&gt;Description: Pop data off the end of the list in chunks of 1,000 and process it before storing it in the relational database. Scheduled to run at a fixed interval (i.e. every 10 minutes).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Redis&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;rpop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'impressions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;json_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// do some data processing...&lt;/span&gt;
                &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;mapToGroups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// maybe summarize high polarity data&lt;/span&gt;
                &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;Impression&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;createMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;While these examples aren't exactly functional, they do accurately depict how simple it is to get started using Redis Lists for fast data ingestion as a means to scaling your relational database driven application without introducing any unnecessary complexity. &lt;/p&gt;

&lt;p&gt;If there's one thing to take away from this article, aside from how powerful Redis is, the next time you're facing a scaling problem or technical limitation due to the constraints of your stack, consider exploring what is already available to you rather than introducing any additional complexity.&lt;/p&gt;

&lt;h3&gt;
  
  
  But wait...
&lt;/h3&gt;

&lt;p&gt;If you’re curious about how far you can push this, I have easily scaled apps to &lt;em&gt;tens of thousands&lt;/em&gt; of writes/second with a single 4gb Redis instance. Redis’ enterprise offering claims to be able to support up to 200m writes/second should you need that.&lt;/p&gt;

&lt;p&gt;Like the content? Feel free to follow and interact with me on &lt;a href="https://twitter.com/colson_ryan"&gt;Twitter&lt;/a&gt; where I share all of my articles, packages, and occasional opinions (&lt;em&gt;mostly&lt;/em&gt; technical).&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>redis</category>
    </item>
    <item>
      <title>The power of static in PHP</title>
      <dc:creator>Ryan</dc:creator>
      <pubDate>Mon, 23 Sep 2019 18:42:08 +0000</pubDate>
      <link>https://dev.to/ryancco/the-power-of-static-in-php-4130</link>
      <guid>https://dev.to/ryancco/the-power-of-static-in-php-4130</guid>
      <description>&lt;h1&gt;
  
  
  The power of static
&lt;/h1&gt;

&lt;p&gt;Although most have probably seen the &lt;code&gt;static&lt;/code&gt; keyword used in PHP before, have you ever wondered how many different ways it can be used? Or possibly how many ways these different uses can provide value to your code? Let's dive into uses and examples and find out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method signature
&lt;/h2&gt;

&lt;p&gt;Arguably the most common usage of the static keyword is the static method. While static methods can be referred to using the object operator (&lt;code&gt;-&amp;gt;&lt;/code&gt;) it is recommended to use the scope resolution operator (&lt;code&gt;::&lt;/code&gt;) as the alternative is deprecated and may be removed in the future. The scope resolution operator allows you to call static methods directly on the class rather than an instance of the class. This also results in the &lt;code&gt;$this&lt;/code&gt; keyword being unavailable in the body of static methods.&lt;/p&gt;

&lt;p&gt;Static methods can be used to implement the &lt;a href="https://designpatternsphp.readthedocs.io/en/latest/Creational/FactoryMethod/README.html"&gt;Factory Method Pattern&lt;/a&gt; which acts as a factory for producing new instances of the containing class each time the method is called.&lt;/p&gt;

&lt;p&gt;In this example we have created a factory method &lt;code&gt;fromArray&lt;/code&gt; which instantiates a &lt;code&gt;User&lt;/code&gt; object, assigns the instance values from the array and returns the instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fromArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'first'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'last'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;password_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="no"&gt;PASSWORD_BCRYPT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fromArray&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="s1"&gt;'first'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'ryan'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'last'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'cco'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'password'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'password'&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, this logic can be extracted to a standalone class which is also known as the &lt;a href="https://designpatternsphp.readthedocs.io/en/latest/Creational/StaticFactory/README.html"&gt;Static Factory Pattern&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserFactory&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UserFactory&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Properties
&lt;/h2&gt;

&lt;p&gt;Unlike regular properties, changing the value of a static property during the execution of your program will affect all instances of the containing class. Even those which have yet to be instantiated. In this way, static properties can be thought of as "mutable class constants". Static properties are only able to be referenced using the scope resolution operator.&lt;/p&gt;

&lt;p&gt;Due to the nature of static properties, they can be used to implement the &lt;a href="https://designpatternsphp.readthedocs.io/en/latest/Creational/Singleton/README.html"&gt;Singleton Pattern&lt;/a&gt;. The Singleton Pattern maintains a single instance of a given class throughout the execution of your program.&lt;/p&gt;

&lt;p&gt;In this example, the initial call to &lt;code&gt;Queue::getInstance()&lt;/code&gt; will create and assign an instance of &lt;code&gt;Queue&lt;/code&gt; to &lt;code&gt;Queue::$instance&lt;/code&gt; and return it. Every subsequent call will return the same instance of &lt;code&gt;Queue&lt;/code&gt; previously assigned to &lt;code&gt;Queue::$instance&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nv"&gt;$instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nv"&gt;$instance&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nv"&gt;$instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nv"&gt;$instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;When in context of a function, static variables maintain their value even once the program has moved out of scope of the containing function. When in the context of a class method, static variables have additional behavior similar to static properties in that changes to their values reflect across all instances of the containing class. Although similar in behavior to static properties, static variables are only accessible within a function or method body.&lt;/p&gt;

&lt;p&gt;Static variables are often used as part of an optimization technique known as &lt;a href="https://eddmann.com/posts/implementing-and-using-memoization-in-php"&gt;Memoization&lt;/a&gt;. Memoization aims to speed up an otherwise expensive operation by caching the results and saving them for a later call with the same parameters.&lt;/p&gt;

&lt;p&gt;In this example, we are creating a hash which is unique to the parameters provided and use it to uniquely identify the call as &lt;code&gt;$key&lt;/code&gt;. If the value of &lt;code&gt;$key&lt;/code&gt; is not found as an index on &lt;code&gt;$cache&lt;/code&gt;, we perform the &lt;code&gt;preg_replace&lt;/code&gt; and store its output on the &lt;code&gt;$key&lt;/code&gt; index of &lt;code&gt;$cache&lt;/code&gt;. Every subsequent call to &lt;code&gt;replace&lt;/code&gt; with the same parameters will bypass the call to &lt;code&gt;preg_replace&lt;/code&gt; and return the value from a previous call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$replacement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$subject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nv"&gt;$cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="nv"&gt;$key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;md5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;func_get_args&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="k"&gt;isset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;preg_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$replacement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$subject&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Anonymous functions
&lt;/h2&gt;

&lt;p&gt;Like methods, when an anonymous function is defined in the context of a class they are not bound to the containing class and do not have the &lt;code&gt;$this&lt;/code&gt; keyword available.&lt;/p&gt;

&lt;p&gt;Static anonymous functions do not necessarily have any unique uses over regular anonymous functions. It is worth noting that in context of a class, static anonymous functions yield micro performance improvements and may be suggested in any instances which you do not need the containing class to be bound to the anonymous function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PostsCollectionFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;unpublished&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$posts&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;published&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Late Static Binding (LSB)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.php.net/manual/en/language.oop5.late-static-bindings.php"&gt;Late static binding&lt;/a&gt; demonstrates another usage of the static keyword: in the context of inheritance. In this context &lt;code&gt;static&lt;/code&gt; refers to the class being called rather than that which the method was defined (which would be referred to with &lt;code&gt;self&lt;/code&gt; or &lt;code&gt;__CLASS__&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;saySelfValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sayClassValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;__CLASS__&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sayStaticValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;saySelfValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'A'&lt;/span&gt;
&lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;sayStaticValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'A'&lt;/span&gt;
&lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;sayClassValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'A'&lt;/span&gt;

&lt;span class="nv"&gt;$b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;saySelfValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'A'&lt;/span&gt;
&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;sayStaticValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'B'&lt;/span&gt;
&lt;span class="nv"&gt;$b&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;sayClassValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 'A'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Closing
&lt;/h1&gt;

&lt;p&gt;We've covered a few examples, some more contrived than others, of the different uses of the &lt;code&gt;static&lt;/code&gt; keyword in PHP. Although contrived, the use cases are all very real and the value that they can deliver to your code can be hugely impactful. At the very least, familiarizing yourself with these uses and techniques to have available in your toolbox will make you that much stronger of a developer.&lt;/p&gt;

&lt;p&gt;If you have some more real-world examples, possibly from open source projects, that could provide insights for other readers please share links in the comments!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally posted on my personal site at &lt;a href="https://ryanc.co/blog/the-power-of-static"&gt;ryanc.co/blog/the-power-of-static&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Faster Eloquent Chunking</title>
      <dc:creator>Ryan</dc:creator>
      <pubDate>Wed, 11 Sep 2019 21:51:35 +0000</pubDate>
      <link>https://dev.to/ryancco/faster-eloquent-chunking-3lc0</link>
      <guid>https://dev.to/ryancco/faster-eloquent-chunking-3lc0</guid>
      <description>&lt;p&gt;As your applications scale, processing large amounts of database records with Laravel Eloquent can become increasingly difficult. Resulting in out of memory exceptions and overall slowing down your application. Why is that?&lt;/p&gt;

&lt;p&gt;When fetching results from the database you are in turn pulling that data into memory. Take this snippet of code for instance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which results in the following query, loading all records in the posts table into memory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typically for tables with a small number of records, this is absolutely acceptable. However, as you accumulate tens of thousands of posts you will eventually begin hitting memory resource constraints of your webserver.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chunking
&lt;/h3&gt;

&lt;p&gt;A common approach in Laravel is to use Eloquent's (via &lt;a href="https://laravel.com/api/6.x/Illuminate/Database/Concerns/BuildsQueries.html"&gt;BuildsQuery&lt;/a&gt;) &lt;a href="https://laravel.com/api/6.x/Illuminate/Database/Concerns/BuildsQueries.html#method_chunk"&gt;&lt;code&gt;chunk()&lt;/code&gt;&lt;/a&gt; method which fetches a fixed amount of records breaking a larger set into more consumable chunks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this might seem fine there are both improvements and gotchas to be aware of.&lt;/p&gt;

&lt;p&gt;First, imagine the following scenario: you are fetching Post records from the database to update an attribute that you are also using in a where clause&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'published_at'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'published_at'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although contrived, this exemplifies a very real problem where such a query would result in an endless loop as the &lt;code&gt;published_at&lt;/code&gt; attribute will always be less than &lt;code&gt;now()&lt;/code&gt; at the time of the queries next execution (assuming accuracy to the second as with MySQL's &lt;code&gt;timestamp&lt;/code&gt; column type or similar).&lt;/p&gt;

&lt;p&gt;Second, there is the matter of the queries performance and its impact on the database server. The above code would result in a query similar to the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;asc&lt;/span&gt; &lt;span class="k"&gt;limit&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="k"&gt;offset&lt;/span&gt; &lt;span class="mi"&gt;9000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;MySQL is unable to go directly to the offset due to deleted records and additional query constraints and, therefore, this query has to effectively select the first 10,000 records to return only the last 1,000 selected. As you can imagine this will not scale well into many-thousands or even millions of rows. This will cause your database server to use unnecessary resources slowing down all other queries from your application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chunking... but better!
&lt;/h3&gt;

&lt;p&gt;In order to both prevent the unforeseen gotcha and improve database server performance we can use Eloquent's &lt;a href="https://laravel.com/api/6.x/Illuminate/Database/Concerns/BuildsQueries.html#method_chunkById"&gt;chunkById&lt;/a&gt; method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'published_at'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;chunkById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'published_at'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet will result in a query similar to the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;published_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2019-09-11 12:00:00'&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;9000&lt;/span&gt; &lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;asc&lt;/span&gt; &lt;span class="k"&gt;limit&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why is this approach considered "better"?&lt;/p&gt;

&lt;p&gt;a) it allows MySQL to completely skip the first 9000 (assumingly sequential) records&lt;br&gt;
b) we will no longer be re-selecting records which we have already updated due to the id constraint in our where clause&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus - How?! 🤔
&lt;/h3&gt;

&lt;p&gt;Diving into the &lt;code&gt;chunkyById&lt;/code&gt; method of the &lt;code&gt;BuildsQueries&lt;/code&gt; trait we &lt;a href="https://github.com/laravel/framework/blob/6.x/src/Illuminate/Database/Concerns/BuildsQueries.php#L92-L107"&gt;see that&lt;/a&gt; the id of the last record fetched (remember, we're ordering by id in ascending order) is stored and used as a parameter in the next query to be run.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Laravel Eloquent Model UUID Route Keys</title>
      <dc:creator>Ryan</dc:creator>
      <pubDate>Mon, 09 Sep 2019 19:33:22 +0000</pubDate>
      <link>https://dev.to/ryancco/laravel-eloquent-model-uuid-route-keys-4efc</link>
      <guid>https://dev.to/ryancco/laravel-eloquent-model-uuid-route-keys-4efc</guid>
      <description>&lt;p&gt;Without arguing the reason for the packages conception, auto-incrementing keys in URLs are often discussed as an anti-pattern, security flaw, and generally ugly (see &lt;a href="http://blogs.perl.org/users/ovid/2014/11/stop-putting-auto-increment-ids-in-urls.html"&gt;here&lt;/a&gt;, &lt;a href="https://www.clever-cloud.com/blog/engineering/2015/05/20/why-auto-increment-is-a-terrible-idea/"&gt;here&lt;/a&gt;, &lt;a href="https://news.ycombinator.com/item?id=15815327"&gt;here&lt;/a&gt; for a few).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ryancco/laravel-uuid-models"&gt;Laravel UUID Models&lt;/a&gt; provides a trait that, when applied to a Laravel Eloquent Model, seamlessly handles allowing it to be routed using UUID's (rather than auto-incrementing ID's).&lt;/p&gt;

&lt;p&gt;Additionally, there are no further code changes required for generating the &lt;a href="https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/"&gt;time ordered UUID&lt;/a&gt;, validation, type-casting, or editing any existing routes or links*. It's as simple as installing the package, placing the trait on the eloquent models you would like to use UUID route keys, and adding a migration to store the UUID field.&lt;/p&gt;

&lt;p&gt;Any suggestions or changes are always encouraged and welcomed on the &lt;a href="https://github.com/ryancco/laravel-uuid-models"&gt;repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;* All routes will work assuming they're generated with the &lt;code&gt;route()&lt;/code&gt; helper.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
  </channel>
</rss>
