Collections
The Illuminate\Support\Collection
class provides a fluent, convenient wrapper for
working with arrays of data. For example, check out the following code. We’ll use the
collect
helper to create a new collection instance from the array, run the
strtoupper
function on each element, and then remove all empty elements:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">abigail</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #89DDFF;">?</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">strtoupper</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">name</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reject</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">empty</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">name</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
As you can see, the Collection
class allows you to chain its methods to perform
fluent mapping and reducing of the underlying array. In general, collections are immutable,
meaning every Collection
method returns an entirely new Collection
instance.
As mentioned above, the collect
helper returns a new
Illuminate\Support\Collection
instance for the given array. So, creating a
collection is as simple as:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div>
The results of Eloquent queries are always returned as
Collection
instances.
Collections are «macroable», which allows you to add additional methods to the
Collection
class at run time. The Illuminate\Support\Collection
class’
macro
method accepts a closure that will be executed when your macro is called. The
macro closure may access the collection’s other methods via $this
, just as if it
were a real method of the collection class. For example, the following code adds a
toUpper
method to the Collection
class:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Str</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">macro</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">toUpper</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Str</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">upper</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;"> });</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">second</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$upper</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toUpper</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['FIRST', 'SECOND']</span></div>
Typically, you should declare collection macros in the boot
method of a service provider.
If necessary, you may define macros that accept additional arguments:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Lang</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">macro</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">toLocale</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$locale</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #BEC5D4;">$locale</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Lang</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, [], </span><span style="color: #BEC5D4;">$locale</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;"> });</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">second</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$translated</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toLocale</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">es</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>
For the majority of the remaining collection documentation, we’ll discuss each method available
on the Collection
class. Remember, all of these methods may be chained to fluently
manipulate the underlying array. Furthermore, almost every method returns a new
Collection
instance, allowing you to preserve the original copy of the collection
when necessary:
The after
method returns the item after the given item. null
is
returned if the given item is not found or is the last item:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> null</span></div>
This method searches for the given item using «loose» comparison, meaning a string containing an
integer value will be considered equal to an integer of the same value. To use «strict»
comparison, you may provide the strict
argument to the method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">4</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, strict: </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> null</span></div>
Alternatively, you may provide your own closure to search for the first item that passes a given
truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 8</span></div>
The all
method returns the underlying array represented by the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3]</span></div>
Alias for the avg
method.
The avg
method returns the average
value of a given key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$average</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">avg</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 20</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$average</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">avg</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div>
The before
method is the opposite of the after
method. It returns the item before the given
item. null
is returned if the given item is not found or is the first item:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> null</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">4</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, strict: </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> null</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4</span></div>
The chunk
method breaks the collection into multiple, smaller collections of a given
size:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunk</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [[1, 2, 3, 4], [5, 6, 7]]</span></div>
This method is especially useful in view
s when working with
a grid system such as Bootstrap.
For example, imagine you have a collection of Eloquent
model
s you want to display in a grid:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$products</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunk</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">) </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">row</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$product</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">col-xs-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$product</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endforeach</span></div>
The chunkWhile
method breaks the collection into multiple, smaller collections based
on the evaluation of the given callback. The $chunk
variable passed to the closure
may be used to inspect the previous element:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">str_split</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">AABBCCCD</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">));</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunkWhile</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$chunk</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">last</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]</span></div>
The collapse
method collapses a collection of arrays into a single, flat collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collapsed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">collapse</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collapsed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></div>
The collect
method returns a new Collection
instance with the items
currently in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collectionA</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collectionB</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collectionA</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collectionB</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3]</span></div>
The collect
method is primarily useful for converting lazy collections into standard Collection
instances:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">make</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">yield</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">yield</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">yield</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 'Illuminate\Support\Collection'</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3]</span></div>
The collect
method is especially useful when you have an instance of
Enumerable
and need a non-lazy collection instance. Since
collect()
is part of the Enumerable
contract, you can safely use
it to get a Collection
instance.
The combine
method combines the values of the collection, as keys, with the values
of another array or collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$combined</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">combine</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">George</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">29</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$combined</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'George', 'age' => 29]</span></div>
The concat
method appends the given array
or collection’s values onto
the end of another collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$concatenated</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">concat</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">concat</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Johnny Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$concatenated</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe', 'Jane Doe', 'Johnny Doe']</span></div>
The concat
method numerically reindexes keys for items concatenated onto the
original collection. To maintain keys in associative collections, see the merge method.
The contains
method determines whether the collection contains a given item. You may
pass a closure to the contains
method to determine if an element exists in the
collection matching a given truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
Alternatively, you may pass a string to the contains
method to determine whether the
collection contains a given item value:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">New York</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
You may also pass a key / value pair to the contains
method, which will determine if
the given pair exists in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
The contains
method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
containsStrict
method to filter using «strict»
comparisons.
For the inverse of contains
, see the doesntContain method.
The containsOneItem
method determines whether the collection contains a single item:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">containsOneItem</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">containsOneItem</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">containsOneItem</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
This method has the same signature as the contains
method; however, all values are compared using «strict» comparisons.
The count
method returns the total number of items in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4</span></div>
The countBy
method counts the occurrences of values in the collection. By default,
the method counts the occurrences of every element, allowing you to count certain «types» of
elements in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$counted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">countBy</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$counted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1 => 1, 2 => 3, 3 => 1]</span></div>
You pass a closure to the countBy
method to count all items by a custom value:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="84e5e8ede7e1c4e3e9e5ede8aae7ebe9" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="9af8f5f8dae3fbf2f5f5b4f9f5f7" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="1b787a697774685b7c767a727735787476" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$counted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">countBy</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$email</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">substr</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">strrchr</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">email</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">@</span><span style="color: #D9F5DD;">"</span><span style="color: #BFC7D5;">),</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$counted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['gmail.com' => 2, 'yahoo.com' => 1]</span></div>
The crossJoin
method cross joins the collection’s values among the given arrays or
collections, returning a Cartesian product with all possible permutations:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$matrix</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">crossJoin</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$matrix</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> [1, 'a'],</span></div><div class="line"><span style="color: #697098;"> [1, 'b'],</span></div><div class="line"><span style="color: #697098;"> [2, 'a'],</span></div><div class="line"><span style="color: #697098;"> [2, 'b'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$matrix</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">crossJoin</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">I</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">II</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$matrix</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> [1, 'a', 'I'],</span></div><div class="line"><span style="color: #697098;"> [1, 'a', 'II'],</span></div><div class="line"><span style="color: #697098;"> [1, 'b', 'I'],</span></div><div class="line"><span style="color: #697098;"> [1, 'b', 'II'],</span></div><div class="line"><span style="color: #697098;"> [2, 'a', 'I'],</span></div><div class="line"><span style="color: #697098;"> [2, 'a', 'II'],</span></div><div class="line"><span style="color: #697098;"> [2, 'b', 'I'],</span></div><div class="line"><span style="color: #697098;"> [2, 'b', 'II'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The dd
method dumps the collection’s items and ends execution of the script:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">dd</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> Collection {</span></div><div class="line"><span style="color: #697098;"> #items: array:2 [</span></div><div class="line"><span style="color: #697098;"> 0 => "John Doe"</span></div><div class="line"><span style="color: #697098;"> 1 => "Jane Doe"</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;"> }</span></div><div class="line"><span style="color: #697098;">*/</span></div>
If you do not want to stop executing the script, use the dump
method instead.
The diff
method compares the collection against another collection or a plain PHP
array
based on its values. This method will return the values in the original
collection that are not present in the given collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diff</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 3, 5]</span></div>
The diffAssoc
method compares the collection against another collection or a plain
PHP array
based on its keys and values. This method will return the key / value
pairs in the original collection that are not present in the given collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orange</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">fruit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">remain</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diffAssoc</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">yellow</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">fruit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">remain</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">used</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['color' => 'orange', 'remain' => 6]</span></div>
Unlike diffAssoc
, diffAssocUsing
accepts a user supplied callback
function for the indices comparison:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orange</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">fruit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">remain</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diffAssocUsing</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Color</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">yellow</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Type</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">fruit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Remain</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">], </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">strnatcasecmp</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['color' => 'orange', 'remain' => 6]</span></div>
The callback must be a comparison function that returns an integer less than, equal to, or
greater than zero. For more information, refer to the PHP documentation on array_diff_uassoc
,
which is the PHP function that the diffAssocUsing
method utilizes internally.
The diffKeys
method compares the collection against another collection or a plain
PHP array
based on its keys. This method will return the key / value pairs in the
original collection that are not present in the given collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">one</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">two</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">three</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">30</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">four</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">five</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">50</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diffKeys</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">two</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">four</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">six</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">eight</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['one' => 10, 'three' => 30, 'five' => 50]</span></div>
The doesntContain
method determines whether the collection does not contain a given
item. You may pass a closure to the doesntContain
method to determine if an element
does not exist in the collection matching a given truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
Alternatively, you may pass a string to the doesntContain
method to determine
whether the collection does not contain a given item value:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Table</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
You may also pass a key / value pair to the doesntContain
method, which will
determine if the given pair does not exist in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div>
The doesntContain
method uses «loose» comparisons when checking item values, meaning
a string with an integer value will be considered equal to an integer of the same value.
The dot
method flattens a multi-dimensional collection into a single level
collection that uses «dot» notation to indicate depth:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$flattened</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">dot</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$flattened</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['products.desk.price' => 100]</span></div>
The dump
method dumps the collection’s items:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">dump</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> Collection {</span></div><div class="line"><span style="color: #697098;"> #items: array:2 [</span></div><div class="line"><span style="color: #697098;"> 0 => "John Doe"</span></div><div class="line"><span style="color: #697098;"> 1 => "Jane Doe"</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;"> }</span></div><div class="line"><span style="color: #697098;">*/</span></div>
If you want to stop executing the script after dumping the collection, use the dd
method instead.
The duplicates
method retrieves and returns duplicate values from the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">duplicates</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2 => 'a', 4 => 'b']</span></div>
If the collection contains arrays or objects, you can pass the key of the attributes that you
wish to check for duplicate values:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$employees</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="016063686660686d416479606c716d642f626e6c" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">position</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Developer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="680209050d1b280d10090518040d460b0705" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">position</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Designer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="75031c16011a071c1435100d14180519105b161a18" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">position</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Developer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$employees</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">duplicates</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">position</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2 => 'Developer']</span></div>
This method has the same signature as the duplicates
method; however, all values are
compared using «strict» comparisons.
The each
method iterates over the items in the collection and passes each item to a
closure:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
If you would like to stop iterating through the items, you may return false
from
your closure:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #697098;">/*</span><span style="color: #697098;"> condition </span><span style="color: #697098;">*/</span><span style="color: #BFC7D5;">) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;"> }</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
The eachSpread
method iterates over the collection’s items, passing each nested item
value into the given callback:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">35</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">33</span><span style="color: #BFC7D5;">]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">eachSpread</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$age</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
You may stop iterating through the items by returning false
from the callback:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">eachSpread</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$age</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
The ensure
method may be used to verify that all elements of a collection are of a
given type or list of types. Otherwise, an UnexpectedValueException
will be thrown:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ensure</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ensure</span><span style="color: #BFC7D5;">([</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Customer</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">]);</span></div>
Primitive types such as string
, int
, float
,
bool
, and array
may also be specified:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ensure</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">int</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>
The ensure
method does not guarantee that elements of different types will not
be added to the collection at a later time.
The every
method may be used to verify that all elements of a collection pass a
given truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">every</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
If the collection is empty, the every
method will return true:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">every</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div>
The except
method returns all items in the collection except for those with the
specified keys:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> false</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">except</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1]</span></div>
For the inverse of except
, see the only method.
The filter
method filters the collection using the given callback, keeping only
those items that pass a given truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4]</span></div>
If no callback is supplied, all entries of the collection that are equivalent to
false
will be removed:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> false</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">''</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3]</span></div>
For the inverse of filter
, see the reject method.
The first
method returns the first element in the collection that passes a given
truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 3</span></div>
You may also call the first
method with no arguments to get the first element in the
collection. If the collection is empty, null
is returned:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>
The firstOrFail
method is identical to the first
method; however, if no
result is found, an Illuminate\Support\ItemNotFoundException
exception will be
thrown:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstOrFail</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Throws ItemNotFoundException...</span></div>
You may also call the firstOrFail
method with no arguments to get the first element
in the collection. If the collection is empty, an
Illuminate\Support\ItemNotFoundException
exception will be thrown:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstOrFail</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Throws ItemNotFoundException...</span></div>
The firstWhere
method returns the first element in the collection with the given key
/ value pair:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Regena</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Linda</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">14</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Diego</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">23</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Linda</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">84</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstWhere</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Linda</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Linda', 'age' => 14]</span></div>
You may also call the firstWhere
method with a comparison operator:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstWhere</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">>=</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">18</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Diego', 'age' => 23]</span></div>
Like the where method, you may pass one argument to the
firstWhere
method. In this scenario, the firstWhere
method will return
the first item where the given item key’s value is «truthy»:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstWhere</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Linda', 'age' => 14]</span></div>
The flatMap
method iterates through the collection and passes each value to the
given closure. The closure is free to modify the item and return it, thus forming a new
collection of modified items. Then, the array is flattened by one level:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sally</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">school</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Arkansas</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">28</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$flattened</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">flatMap</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$values</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">array_map</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">strtoupper</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">values</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$flattened</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' => '28'];</span></div>
The flatten
method flattens a multi-dimensional collection into a single dimension:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">languages</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>php
', 'javascript'
]
]);
$flattened = $collection->flatten();
$flattened->all();
// ['taylor', 'php
', 'javascript'];
If necessary, you may pass the flatten
method a «depth» argument:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">iPhone 6S</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Samsung</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Galaxy S7</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Samsung</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$products</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">flatten</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$products</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'iPhone 6S', 'brand' => 'Apple'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Galaxy S7', 'brand' => 'Samsung'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
In this example, calling flatten
without providing the depth would have also
flattened the nested arrays, resulting in
['iPhone 6S', 'Apple', 'Galaxy S7', 'Samsung']
. Providing a depth allows you to
specify the number of levels nested arrays will be flattened.
The flip
method swaps the collection’s keys with their corresponding values:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">framework</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel
']);
$flipped = $collection->flip();
$flipped->all();
// ['taylor' => 'name', 'laravel
' => 'framework']
The forget
method removes an item from the collection by its key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">framework</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel
']);
// Forget a single key...
$collection->forget('name');
// ['framework' => 'laravel
']
// Forget multiple keys...
$collection->forget(['name', 'framework']);
// []
Unlike most other collection methods, forget
does not return a new modified
collection; it modifies and returns the collection it is called on.
The forPage
method returns a new collection containing the items that would be
present on a given page number. The method accepts the page number as its first argument and the
number of items to show per page as its second argument:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">forPage</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [4, 5, 6]</span></div>
The get
method returns the item at a given key. If the key does not exist,
null
is returned:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">framework</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel
']);
$value = $collection->get('name');
// taylor
You may optionally pass a default value as the second argument:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">framework</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel
']);
$value = $collection->get('age', 34);
// 34
You may even pass a callback as the method’s default value. The result of the callback will be
returned if the specified key does not exist:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="ed998c9481829fad88958c809d8188c38e8280" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> <a class="__cf_email__" data-cfemail="4a3e2b332625380a2f322b273a262f64292527" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span></div>
The groupBy
method groups the collection’s items by a given key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account-x10</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account-x10</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account-x11</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">groupBy</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 'account-x10' => [</span></div><div class="line"><span style="color: #697098;"> ['account_id' => 'account-x10', 'product' => 'Chair'],</span></div><div class="line"><span style="color: #697098;"> ['account_id' => 'account-x10', 'product' => 'Bookcase'],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> 'account-x11' => [</span></div><div class="line"><span style="color: #697098;"> ['account_id' => 'account-x11', 'product' => 'Desk'],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
Instead of passing a string key
, you may pass a callback. The callback should return
the value you wish to key the group by:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">groupBy</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">substr</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">-</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 'x10' => [</span></div><div class="line"><span style="color: #697098;"> ['account_id' => 'account-x10', 'product' => 'Chair'],</span></div><div class="line"><span style="color: #697098;"> ['account_id' => 'account-x10', 'product' => 'Bookcase'],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> 'x11' => [</span></div><div class="line"><span style="color: #697098;"> ['account_id' => 'account-x11', 'product' => 'Desk'],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
Multiple grouping criteria may be passed as an array. Each array element will be applied to the
corresponding level within a multi-dimensional array:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">roles</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_3</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">roles</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">30</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">roles</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">roles</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$data</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">groupBy</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">roles</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class="line"><span style="color: #BFC7D5;">}], preserveKeys: </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;">[</span></div><div class="line"><span style="color: #697098;"> 1 => [</span></div><div class="line"><span style="color: #697098;"> 'Role_1' => [</span></div><div class="line"><span style="color: #697098;"> 10 => ['user' => 1, 'skill' => 1, 'roles' => ['Role_1', 'Role_3']],</span></div><div class="line"><span style="color: #697098;"> 20 => ['user' => 2, 'skill' => 1, 'roles' => ['Role_1', 'Role_2']],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> 'Role_2' => [</span></div><div class="line"><span style="color: #697098;"> 20 => ['user' => 2, 'skill' => 1, 'roles' => ['Role_1', 'Role_2']],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> 'Role_3' => [</span></div><div class="line"><span style="color: #697098;"> 10 => ['user' => 1, 'skill' => 1, 'roles' => ['Role_1', 'Role_3']],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> 2 => [</span></div><div class="line"><span style="color: #697098;"> 'Role_1' => [</span></div><div class="line"><span style="color: #697098;"> 30 => ['user' => 3, 'skill' => 2, 'roles' => ['Role_1']],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> 'Role_2' => [</span></div><div class="line"><span style="color: #697098;"> 40 => ['user' => 4, 'skill' => 2, 'roles' => ['Role_2']],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;">];</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The has
method determines if a given key exists in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">amount</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">amount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">amount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
The hasAny
method determines whether any of the given keys exist in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">amount</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasAny</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasAny</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
The implode
method joins items in a collection. Its arguments depend on the type of
items in the collection. If the collection contains arrays or objects, you should pass the key
of the attributes you wish to join, and the «glue» string you wish to place between the values:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">implode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Desk, Chair</span></div>
If the collection contains simple strings or numeric values, you should pass the «glue» as the
only argument to the method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">implode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">-</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> '1-2-3-4-5'</span></div>
You may pass a closure to the implode
method if you would like to format the values
being imploded:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">implode</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">strtoupper</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;">}, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> DESK, CHAIR</span></div>
The intersect
method removes any values from the original collection that are not
present in the given array
or collection. The resulting collection will preserve
the original collection’s keys:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sofa</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">intersect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [0 => 'Desk', 2 => 'Chair']</span></div>
The intersectAssoc
method compares the original collection against another
collection or array
, returning the key / value pairs that are present in all of the
given collections:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">red</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">size</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">M</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">material</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">cotton</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">intersectAssoc</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">blue</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">size</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">M</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">material</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">polyester</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['size' => 'M']</span></div>
The intersectByKeys
method removes any keys and their corresponding values from the
original collection that are not present in the given array
or collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">serial</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">UX301</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">screen</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">year</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2009</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">intersectByKeys</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">reference</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">UX404</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">tab</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">year</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2011</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['type' => 'screen', 'year' => 2009]</span></div>
The isEmpty
method returns true
if the collection is empty; otherwise,
false
is returned:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isEmpty</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div>
The isNotEmpty
method returns true
if the collection is not empty;
otherwise, false
is returned:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isNotEmpty</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
The join
method joins the collection’s values with a string. Using this method’s
second argument, you may also specify how the final element should be appended to the string:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 'a, b, c'</span></div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, and </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 'a, b, and c'</span></div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"> and </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 'a and b'</span></div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"> and </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 'a'</span></div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"> and </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> ''</span></div>
The keyBy
method keys the collection by the given key. If multiple items have the
same key, only the last one will appear in the new collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-200</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keyed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">keyBy</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keyed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],</span></div><div class="line"><span style="color: #697098;"> 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
You may also pass a callback to the method. The callback should return the value to key the
collection by:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$keyed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">keyBy</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">strtoupper</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keyed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 'PROD-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],</span></div><div class="line"><span style="color: #697098;"> 'PROD-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The keys
method returns all of the collection’s keys:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-200</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-200</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keys</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">keys</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keys</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['prod-100', 'prod-200']</span></div>
The last
method returns the last element in the collection that passes a given truth
test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">last</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div>
You may also call the last
method with no arguments to get the last element in the
collection. If the collection is empty, null
is returned:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">last</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4</span></div>
The lazy
method returns a new LazyCollection
instance from the underlying array
of items:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">lazy</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Illuminate\Support\LazyCollection</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4]</span></div>
This is especially useful when you need to perform transformations on a huge
Collection
that contains many items:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$count</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$hugeCollection</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">lazy</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">country</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">FR</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">balance</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">100</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">();</span></div>
By converting the collection to a LazyCollection
, we avoid having to allocate a ton
of additional memory. Though the original collection still keeps its values in memory,
the subsequent filters will not. Therefore, virtually no additional memory will be allocated
when filtering the collection’s results.
The static macro
method allows you to add methods to the Collection
class at run time. Refer to the documentation on extending
collections for more information.
The static make
method creates a new collection instance. See the Creating Collections section.
The map
method iterates through the collection and passes each value to the given
callback. The callback is free to modify the item and return it, thus forming a new collection
of modified items:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$multiplied</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$multiplied</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 4, 6, 8, 10]</span></div>
Like most other collection methods, map
returns a new collection instance; it
does not modify the collection it is called on. If you want to transform the original
collection, use the transform
method.
The mapInto()
method iterates over the collection, creating a new instance of the
given class by passing the value into the constructor:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">Currency</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Create a new currency instance.</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">__construct</span><span style="color: #D9F5DD;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$code</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {}</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">USD</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">EUR</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">GBP</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$currencies</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapInto</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Currency</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$currencies</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [Currency('USD'), Currency('EUR'), Currency('GBP')]</span></div>
The mapSpread
method iterates over the collection’s items, passing each nested item
value into the given closure. The closure is free to modify the item and return it, thus forming
a new collection of modified items:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunk</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sequence</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapSpread</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$even</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$odd</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$even</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$odd</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sequence</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 5, 9, 13, 17]</span></div>
The mapToGroups
method groups the collection’s items by the given closure. The
closure should return an associative array containing a single key / value pair, thus forming a
new collection of grouped values:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sales</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sales</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Johnny Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Marketing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapToGroups</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> [</span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]];</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 'Sales' => ['John Doe', 'Jane Doe'],</span></div><div class="line"><span style="color: #697098;"> 'Marketing' => ['Johnny Doe'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sales</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe', 'Jane Doe']</span></div>
The mapWithKeys
method iterates through the collection and passes each value to the
given callback. The callback should return an associative array containing a single key / value
pair:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sales</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="c3a9acabad83a6bba2aeb3afa6eda0acae" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Marketing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="402a212e25002538212d302c256e232f2d" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keyed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapWithKeys</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> [</span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]];</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keyed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> '<a class="__cf_email__" data-cfemail="6e040106002e0b160f031e020b400d0103" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>' => 'John',</span></div><div class="line"><span style="color: #697098;"> '<a class="__cf_email__" data-cfemail="f399929d96b3968b929e839f96dd909c9e" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>' => 'Jane',</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The max
method returns the maximum value of a given key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$max</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">max</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 20</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$max</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">max</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 5</span></div>
The median
method returns the median
value of a given key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$median</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">median</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 15</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$median</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">median</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1.5</span></div>
The merge
method merges the given array or collection with the original collection.
If a string key in the given items matches a string key in the original collection, the given
item’s value will overwrite the value in the original collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1, 'price' => 200, 'discount' => false]</span></div>
If the given item’s keys are numeric, the values will be appended to the end of the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Desk', 'Chair', 'Bookcase', 'Door']</span></div>
The mergeRecursive
method merges the given array or collection recursively with the
original collection. If a string key in the given items matches a string key in the original
collection, then the values for these keys are merged together into an array, and this is done
recursively:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mergeRecursive</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => [1, 2], 'price' => [100, 200], 'discount' => false]</span></div>
The min
method returns the minimum value of a given key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$min</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">]])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">min</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$min</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">min</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>
The mode
method returns the mode value of a given key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$mode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [10]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$mode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mode</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$mode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mode</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div>
The multiply
method creates the specified number of copies of all items in the
collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">User #1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="fa8f899f88cbba9f829b978a969fd4999597" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">User #2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="b9cccadccb8bf9dcc1d8d4c9d5dc97dad6d4" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">multiply</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'User #1', 'email' => '<a class="__cf_email__" data-cfemail="1f6a6c7a6d2e5f7a677e726f737a317c7072" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'User #2', 'email' => '<a class="__cf_email__" data-cfemail="33464056410173564b525e435f561d505c5e" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'User #1', 'email' => '<a class="__cf_email__" data-cfemail="47323422357607223f262a372b226924282a" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'User #2', 'email' => '<a class="__cf_email__" data-cfemail="82f7f1e7f0b0c2e7fae3eff2eee7ace1edef" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'User #1', 'email' => '<a class="__cf_email__" data-cfemail="3d484e584f0c7d58455c504d5158135e5250" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'User #2', 'email' => '<a class="__cf_email__" data-cfemail="7c090f190e4e3c19041d110c1019521f1311" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The nth
method creates a new collection consisting of every n-th element:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">d</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">e</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">f</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">nth</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['a', 'e']</span></div>
You may optionally pass a starting offset as the second argument:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">nth</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['b', 'f']</span></div>
The only
method returns the items in the collection with the specified keys:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> false</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">only</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1, 'name' => 'Desk']</span></div>
For the inverse of only
, see the except method.
The pad
method will fill the array with the given value until the array reaches the
specified size. This method behaves like the array_pad PHP function.
To pad to the left, you should specify a negative size. No padding will take place if the
absolute value of the given size is less than or equal to the length of the array:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">A</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">B</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">C</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pad</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['A', 'B', 'C', 0, 0]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pad</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">-</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [0, 0, 'A', 'B', 'C']</span></div>
The partition
method may be combined with PHP array destructuring to separate
elements that pass a given truth test from those that do not:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$underThree</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$equalOrAboveThree</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">partition</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$i</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$i</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$underThree</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$equalOrAboveThree</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4, 5, 6]</span></div>
The percentage
method may be used to quickly determine the percentage of items in
the collection that pass a given truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$percentage</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">percentage</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 33.33</span></div>
By default, the percentage will be rounded to two decimal places. However, you may customize this
behavior by providing a second argument to the method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$percentage</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">percentage</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, precision: </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 33.333</span></div>
The pipe
method passes the collection to the given closure and returns the result of
the executed closure:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$piped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pipe</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 6</span></div>
The pipeInto
method creates a new instance of the given class and passes the
collection into the constructor:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">ResourceCollection</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Create a new ResourceCollection instance.</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">__construct</span><span style="color: #D9F5DD;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {}</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$resource</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pipeInto</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">ResourceCollection</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$resource</span><span style="color: #89DDFF;">->collection-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3]</span></div>
The pipeThrough
method passes the collection to the given array of closures and
returns the result of the executed closures:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pipeThrough</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;"> },</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;"> },</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 15</span></div>
The pluck
method retrieves all of the values for a given key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-200</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Desk', 'Chair']</span></div>
You may also specify how you wish the resulting collection to be keyed:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['prod-100' => 'Desk', 'prod-200' => 'Chair']</span></div>
The pluck
method also supports retrieving nested values using «dot» notation:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Laracon</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">speakers</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first_day</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Rosa</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Judith</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">VueConf</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">speakers</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first_day</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Joey</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">speakers.first_day</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [['Rosa', 'Judith'], ['Abigail', 'Joey']]</span></div>
If duplicate keys exist, the last matching element will be inserted into the plucked collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Tesla</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">red</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Pagani</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">white</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Tesla</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Pagani</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orange</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Tesla' => 'black', 'Pagani' => 'orange']</span></div>
The pop
method removes and returns the last item from the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pop</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 5</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4]</span></div>
You may pass an integer to the pop
method to remove and return multiple items from
the end of a collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pop</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> collect([5, 4, 3])</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div>
The prepend
method adds an item to the beginning of the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">prepend</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [0, 1, 2, 3, 4, 5]</span></div>
You may also pass a second argument to specify the key of the prepended item:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">one</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">two</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">prepend</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">zero</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['zero' => 0, 'one' => 1, 'two' => 2]</span></div>
The pull
method removes and returns an item from the collection by its key:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pull</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 'Desk'</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 'prod-100']</span></div>
The push
method appends an item to the end of the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4, 5]</span></div>
The put
method sets the given key and value in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">put</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1, 'name' => 'Desk', 'price' => 100]</span></div>
The random
method returns a random item from the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4 - (retrieved randomly)</span></div>
You may pass an integer to random
to specify how many items you would like to
randomly retrieve. A collection of items is always returned when explicitly passing the number
of items you wish to receive:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$random</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$random</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 4, 5] - (retrieved randomly)</span></div>
If the collection instance has fewer items than requested, the random
method will
throw an InvalidArgumentException
.
The random
method also accepts a closure, which will receive the current collection
instance:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$random</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$items</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #89DDFF;">min</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">items</span><span style="color: #BFC7D5;">)));</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$random</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4, 5] - (retrieved randomly)</span></div>
The range
method returns a collection containing integers between the specified
range:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">range</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4, 5, 6]</span></div>
The reduce
method reduces the collection to a single value, passing the result of
each iteration into the subsequent iteration:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$total</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduce</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #89DDFF;">?</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$carry</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$carry</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 6</span></div>
The value for $carry
on the first iteration is null
; however, you may
specify its initial value by passing a second argument to reduce
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduce</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$carry</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$carry</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}, </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div>
The reduce
method also passes array keys in associative collections to the given
callback:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">usd</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1400</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">gbp</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1200</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">eur</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$ratio</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">usd</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">gbp</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1.37</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">eur</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1.22</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">];</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduce</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$carry</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #BEC5D4;">$ratio</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$carry</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$ratio</span><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$key</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4264</span></div>
The reduceSpread
method reduces the collection to an array of values, passing the
results of each iteration into the subsequent iteration. This method is similar to the
reduce
method; however, it can accept multiple initial values:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$batch</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Image</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">unprocessed</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduceSpread</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$batch</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Image</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$image</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">>=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$image</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">creditsRequired</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$batch</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$image</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">-=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$image</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">creditsRequired</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;"> }</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> [</span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$batch</span><span style="color: #BFC7D5;">];</span></div><div class="line"><span style="color: #BFC7D5;"> }, </span><span style="color: #BEC5D4;">$creditsAvailable</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">());</span></div>
The reject
method filters the collection using the given closure. The closure should
return true
if the item should be removed from the resulting collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reject</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div>
For the inverse of the reject
method, see the filter
method.
The replace
method behaves similarly to merge
; however, in addition to
overwriting matching items that have string keys, the replace
method will also
overwrite items in the collection that have matching numeric keys:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">James</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$replaced</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">replace</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Victoria</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Finn</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$replaced</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Taylor', 'Victoria', 'James', 'Finn']</span></div>
This method works like replace
, but it will recur into arrays and apply the same
replacement process to the inner values:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">James</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Victoria</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Finn</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$replaced</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">replaceRecursive</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Charlie</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">King</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$replaced</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Charlie', 'Abigail', ['James', 'King', 'Finn']]</span></div>
The reverse
method reverses the order of the collection’s items, preserving the
original keys:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">d</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">e</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$reversed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reverse</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$reversed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 4 => 'e',</span></div><div class="line"><span style="color: #697098;"> 3 => 'd',</span></div><div class="line"><span style="color: #697098;"> 2 => 'c',</span></div><div class="line"><span style="color: #697098;"> 1 => 'b',</span></div><div class="line"><span style="color: #697098;"> 0 => 'a',</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The search
method searches the collection for the given value and returns its key if
found. If the item is not found, false
is returned:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">search</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>
The search is done using a «loose» comparison, meaning a string with an integer value will be
considered equal to an integer of the same value. To use «strict» comparison, pass
true
as the second argument to the method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">search</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">4</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, strict: </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div>
Alternatively, you may provide your own closure to search for the first item that passes a given
truth test:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">search</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div>
The select
method selects the given keys from the collection, similar to an SQL
SELECT
statement:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">role</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Developer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">active</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Victoria Faith</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">role</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Researcher</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">active</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">select</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">role</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Taylor Otwell', 'role' => 'Developer'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Victoria Faith', 'role' => 'Researcher'],</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The shift
method removes and returns the first item from the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">shift</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 3, 4, 5]</span></div>
You may pass an integer to the shift
method to remove and return multiple items from
the beginning of a collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">shift</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> collect([1, 2, 3])</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [4, 5]</span></div>
The shuffle
method randomly shuffles the items in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$shuffled</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">shuffle</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$shuffled</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 2, 5, 1, 4] - (generated randomly)</span></div>
The skip
method returns a new collection, with the given number of elements removed
from the beginning of the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skip</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 6, 7, 8, 9, 10]</span></div>
The skipUntil
method skips over items from the collection until the given callback
returns true
and then returns the remaining items in the collection as a new
collection instance:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skipUntil</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">>=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4]</span></div>
You may also pass a simple value to the skipUntil
method to skip all items until the
given value is found:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skipUntil</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4]</span></div>
If the given value is not found or the callback never returns true
, the
skipUntil
method will return an empty collection.
The skipWhile
method skips over items from the collection while the given callback
returns true
and then returns the remaining items in the collection as a new
collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skipWhile</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [4]</span></div>
If the callback never returns false
, the skipWhile
method will
return an empty collection.
The slice
method returns a slice of the collection starting at the given index:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$slice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">slice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$slice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 6, 7, 8, 9, 10]</span></div>
If you would like to limit the size of the returned slice, pass the desired size as the second
argument to the method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$slice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">slice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$slice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 6]</span></div>
The returned slice will preserve keys by default. If you do not wish to preserve the original
keys, you can use the values
method to reindex them.
The sliding
method returns a new collection of chunks representing a «sliding
window» view
of the items in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sliding</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [[1, 2], [2, 3], [3, 4], [4, 5]]</span></div>
This is especially useful in conjunction with the eachSpread
method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$transactions</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sliding</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">eachSpread</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$previous</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$current</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$current</span><span style="color: #89DDFF;">->total</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$previous</span><span style="color: #89DDFF;">->total</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$current</span><span style="color: #89DDFF;">->amount</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
You may optionally pass a second «step» value, which determines the distance between the first
item of every chunk:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sliding</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, step: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [[1, 2, 3], [3, 4, 5]]</span></div>
The sole
method returns the first element in the collection that passes a given
truth test, but only if the truth test matches exactly one element:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sole</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div>
You may also pass a key / value pair to the sole
method, which will return the first
element in the collection that matches the given pair, but only if it exactly one element
matches:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sole</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product' => 'Chair', 'price' => 100]</span></div>
Alternatively, you may also call the sole
method with no argument to get the first
element in the collection if there is only one element:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sole</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product' => 'Desk', 'price' => 200]</span></div>
If there are no elements in the collection that should be returned by the sole
method, an \Illuminate\Collections\ItemNotFoundException
exception will be thrown.
If there is more than one element that should be returned, an
\Illuminate\Collections\MultipleItemsFoundException
will be thrown.
Alias for the contains
method.
The sort
method sorts the collection. The sorted collection keeps the original array
keys, so in the following example we will use the values
method to reset the keys to consecutively
numbered indexes:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sort</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4, 5]</span></div>
If your sorting needs are more advanced, you may pass a callback to sort
with your
own algorithm. Refer to the PHP documentation on uasort
,
which is what the collection’s sort
method calls utilizes internally.
If you need to sort a collection of nested arrays or objects, see the sortBy
and sortByDesc
methods.
The sortBy
method sorts the collection by the given key. The sorted collection keeps
the original array keys, so in the following example we will use the values
method to reset the keys to consecutively
numbered indexes:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Chair', 'price' => 100],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Bookcase', 'price' => 150],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The sortBy
method accepts sort flags as its second
argument:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Item 1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Item 12</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Item 3</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">SORT_NATURAL</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['title' => 'Item 1'],</span></div><div class="line"><span style="color: #697098;"> ['title' => 'Item 3'],</span></div><div class="line"><span style="color: #697098;"> ['title' => 'Item 12'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
Alternatively, you may pass your own closure to determine how to sort the collection’s values:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Mahogany</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Red</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Beige</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Brown</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$product</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">product</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Chair', 'colors' => ['Black']],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
If you would like to sort your collection by multiple attributes, you may pass an array of sort
operations to the sortBy
method. Each sort operation should be an array consisting
of the attribute that you wish to sort by and the direction of the desired sort:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">34</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">36</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">32</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">asc</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desc</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Abigail Otwell', 'age' => 32],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Abigail Otwell', 'age' => 30],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Taylor Otwell', 'age' => 36],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Taylor Otwell', 'age' => 34],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
When sorting a collection by multiple attributes, you may also provide closures that define each
sort operation:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">34</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">36</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">32</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$a</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$b</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$a</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;"><=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$b</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$a</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$b</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$b</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;"><=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$a</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Abigail Otwell', 'age' => 32],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Abigail Otwell', 'age' => 30],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Taylor Otwell', 'age' => 36],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Taylor Otwell', 'age' => 34],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
This method has the same signature as the sortBy
method, but will sort the collection in the opposite order.
This method will sort the collection in the opposite order as the sort
method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortDesc</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 4, 3, 2, 1]</span></div>
Unlike sort
, you may not pass a closure to sortDesc
. Instead, you
should use the sort
method and invert your comparison.
The sortKeys
method sorts the collection by the keys of the underlying associative
array:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">22345</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">last</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortKeys</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 'first' => 'John',</span></div><div class="line"><span style="color: #697098;"> 'id' => 22345,</span></div><div class="line"><span style="color: #697098;"> 'last' => 'Doe',</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
This method has the same signature as the sortKeys
method, but will sort the collection in the opposite order.
The sortKeysUsing
method sorts the collection by the keys of the underlying
associative array using a callback:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">ID</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">22345</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">last</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortKeysUsing</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">strnatcasecmp</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 'first' => 'John',</span></div><div class="line"><span style="color: #697098;"> 'ID' => 22345,</span></div><div class="line"><span style="color: #697098;"> 'last' => 'Doe',</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The callback must be a comparison function that returns an integer less than, equal to, or
greater than zero. For more information, refer to the PHP documentation on uksort
,
which is the PHP function that sortKeysUsing
method utilizes internally.
The splice
method removes and returns a slice of items starting at the specified
index:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4, 5]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div>
You may pass a second argument to limit the size of the resulting collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 4, 5]</span></div>
In addition, you may pass a third argument containing the new items to replace the items removed
from the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, [</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">11</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 10, 11, 4, 5]</span></div>
The split
method breaks a collection into the given number of groups:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$groups</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">split</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$groups</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [[1, 2], [3, 4], [5]]</span></div>
The splitIn
method breaks a collection into the given number of groups, filling
non-terminal groups completely before allocating the remainder to the final group:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$groups</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splitIn</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$groups</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]</span></div>
The sum
method returns the sum of all items in the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 15</span></div>
If the collection contains nested arrays or objects, you should pass a key that will be used to
determine which values to sum:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">JavaScript: The Good Parts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pages</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">176</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">JavaScript: The Definitive Guide</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pages</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1096</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pages</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1272</span></div>
In addition, you may pass your own closure to determine which values of the collection to sum:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Mahogany</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Red</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Beige</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Brown</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$product</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">product</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 6</span></div>
The take
method returns a new collection with the specified number of items:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">take</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [0, 1, 2]</span></div>
You may also pass a negative integer to take the specified number of items from the end of the
collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">take</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">-</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [4, 5]</span></div>
The takeUntil
method returns items in the collection until the given callback
returns true
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntil</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">>=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div>
You may also pass a simple value to the takeUntil
method to get the items until the
given value is found:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntil</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div>
If the given value is not found or the callback never returns true
, the
takeUntil
method will return all items in the collection.
The takeWhile
method returns items in the collection until the given callback
returns false
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeWhile</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2]</span></div>
If the callback never returns false
, the takeWhile
method will
return all items in the collection.
The tap
method passes the collection to the given callback, allowing you to «tap»
into the collection at a specific point and do something with the items while not affecting the
collection itself. The collection is then returned by the tap
method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sort</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">tap</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Log</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">debug</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Values after sorting</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">());</span></div><div class="line"><span style="color: #BFC7D5;"> })</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">shift</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>
The static times
method creates a new collection by invoking the given closure a
specified number of times:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">times</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$number</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [9, 18, 27, 36, 45, 54, 63, 72, 81, 90]</span></div>
The toArray
method converts the collection into a plain PHP array
. If
the collection’s values are Eloquent model
s, the
model
s will also be converted to arrays:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
toArray
also converts all of the collection’s nested objects that are an
instance of Arrayable
to an array. If you want to get the raw array underlying
the collection, use the all
method instead.
The toJson
method converts the collection into a JSON serialized string:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toJson</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> '{"name":"Desk", "price":200}'</span></div>
The transform
method iterates over the collection and calls the given callback with
each item in the collection. The items in the collection will be replaced by the values returned
by the callback:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">transform</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 4, 6, 8, 10]</span></div>
Unlike most other collection methods, transform
modifies the collection itself.
If you wish to create a new collection instead, use the map
method.
The undot
method expands a single-dimensional collection that uses «dot» notation
into a multi-dimensional collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$person</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name.first_name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Marie</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name.last_name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Valentine</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.line_1</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">2992 Eagle Drive</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.line_2</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">''</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.suburb</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Detroit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.state</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">MI</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.postcode</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">48219</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$person</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$person</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">undot</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$person</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> "name" => [</span></div><div class="line"><span style="color: #697098;"> "first_name" => "Marie",</span></div><div class="line"><span style="color: #697098;"> "last_name" => "Valentine",</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> "address" => [</span></div><div class="line"><span style="color: #697098;"> "line_1" => "2992 Eagle Drive",</span></div><div class="line"><span style="color: #697098;"> "line_2" => "",</span></div><div class="line"><span style="color: #697098;"> "suburb" => "Detroit",</span></div><div class="line"><span style="color: #697098;"> "state" => "MI",</span></div><div class="line"><span style="color: #697098;"> "postcode" => "48219",</span></div><div class="line"><span style="color: #697098;"> ],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The union
method adds the given array to the collection. If the given array contains
keys that are already in the original collection, the original collection’s values will be
preferred:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$union</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">union</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">d</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$union</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1 => ['a'], 2 => ['b'], 3 => ['c']]</span></div>
The unique
method returns all of the unique items in the collection. The returned
collection keeps the original array keys, so in the following example we will use the values
method to reset the keys to consecutively
numbered indexes:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unique</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4]</span></div>
When dealing with nested arrays or objects, you may specify the key used to determine uniqueness:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">iPhone 6</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">phone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">iPhone 5</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">phone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple Watch</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">watch</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Galaxy S6</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Samsung</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">phone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Galaxy Gear</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Samsung</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">watch</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unique</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
Finally, you may also pass your own closure to the unique
method to specify which
value should determine an item’s uniqueness:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unique</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]</span><span style="color: #89DDFF;">.</span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The unique
method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
uniqueStrict
method to filter using «strict»
comparisons.
This method has the same signature as the unique
method; however, all values are compared using «strict» comparisons.
The unless
method will execute the given callback unless the first argument given to
the method evaluates to true
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unless</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unless</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 5]</span></div>
A second callback may be passed to the unless
method. The second callback will be
executed when the first argument given to the unless
method evaluates to
true
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unless</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 5]</span></div>
For the inverse of unless
, see the when
method.
Alias for the whenNotEmpty
method.
Alias for the whenEmpty
method.
The static unwrap
method returns the collection’s underlying items from the given
value when applicable:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">unwrap</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">));</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">unwrap</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">unwrap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 'John Doe'</span></div>
The value
method retrieves a given value from the first element of the collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Speaker</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">400</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">value</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 200</span></div>
The values
method returns a new collection with the keys reset to consecutive
integers:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">11</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$values</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$values</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> 0 => ['product' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;"> 1 => ['product' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The when
method will execute the given callback when the first argument given to the
method evaluates to true
. The collection instance and the first argument given to
the when
method will be provided to the closure:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4]</span></div>
A second callback may be passed to the when
method. The second callback will be
executed when the first argument given to the when
method evaluates to
false
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 5]</span></div>
For the inverse of when
, see the unless
method.
The whenEmpty
method will execute the given callback when the collection is empty:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Michael</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Tom</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenEmpty</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Adam</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Michael', 'Tom']</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenEmpty</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Adam</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Adam']</span></div>
A second closure may be passed to the whenEmpty
method that will be executed when
the collection is not empty:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Michael</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Tom</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenEmpty</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Adam</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Michael', 'Tom', 'Taylor']</span></div>
For the inverse of whenEmpty
, see the whenNotEmpty
method.
The whenNotEmpty
method will execute the given callback when the collection is not
empty:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">michael</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">tom</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenNotEmpty</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">adam</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['michael', 'tom', 'adam']</span></div><div class="line"> </div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenNotEmpty</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">adam</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> []</span></div>
A second closure may be passed to the whenNotEmpty
method that will be executed when
the collection is empty:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenNotEmpty</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">adam</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['taylor']</span></div>
For the inverse of whenNotEmpty
, see the whenEmpty
method.
The where
method filters the collection by a given key / value pair:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Chair', 'price' => 100],</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Door', 'price' => 100],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The where
method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
whereStrict
method to filter using «strict»
comparisons.
Optionally, you may pass a comparison operator as the second parameter. Supported operators are:
‘===’, ‘!==’, ‘!=’, ‘==’, ‘=’, ‘<>’, ‘>’, ‘<‘, ‘>=’, and ‘<=’:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jim</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">deleted_at</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">2019-01-01 00:00:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sally</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">deleted_at</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">2019-01-02 00:00:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sue</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">deleted_at</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">deleted_at</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">!=</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
This method has the same signature as the where
method;
however, all values are compared using «strict» comparisons.
The whereBetween
method filters the collection by determining if a specified item
value is within a given range:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">80</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Pencil</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereBetween</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Bookcase', 'price' => 150],</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Door', 'price' => 100],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The whereIn
method removes elements from the collection that do not have a specified
item value that is contained within the given array:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereIn</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Bookcase', 'price' => 150],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The whereIn
method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
whereInStrict
method to filter using «strict»
comparisons.
This method has the same signature as the whereIn
method; however, all values are compared using «strict» comparisons.
The whereInstanceOf
method filters the collection by a given class type:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Post</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereInstanceOf</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [App\Models\User, App\Models\User]</span></div>
The whereNotBetween
method filters the collection by determining if a specified item
value is outside of a given range:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">80</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Pencil</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNotBetween</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Chair', 'price' => 80],</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Pencil', 'price' => 30],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The whereNotIn
method removes elements from the collection that have a specified
item value that is contained within the given array:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNotIn</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Chair', 'price' => 100],</span></div><div class="line"><span style="color: #697098;"> ['product' => 'Door', 'price' => 100],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The whereNotIn
method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
whereNotInStrict
method to filter using
«strict» comparisons.
This method has the same signature as the whereNotIn
method; however, all values are
compared using «strict» comparisons.
The whereNotNull
method returns items from the collection where the given key is not
null
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNotNull</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Desk'],</span></div><div class="line"><span style="color: #697098;"> ['name' => 'Bookcase'],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The whereNull
method returns items from the collection where the given key is
null
:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNull</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">/*</span></div><div class="line"><span style="color: #697098;"> [</span></div><div class="line"><span style="color: #697098;"> ['name' => null],</span></div><div class="line"><span style="color: #697098;"> ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
The static wrap
method wraps the given value in a collection when applicable:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">));</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div>
The zip
method merges together the values of the given array with the values of the
original collection at their corresponding index:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$zipped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">zip</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$zipped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [['Chair', 100], ['Desk', 200]]</span></div>
Collections also provide support for «higher order messages», which are short-cuts for performing
common actions on collections. The collection methods that provide higher order messages are: average
, avg
,
contains
, each
, every
,
filter
, first
, flatMap
, groupBy
, keyBy
, map
, max
, min
, partition
, reject
, skipUntil
, skipWhile
, some
, sortBy
,
sortByDesc
, sum
, takeUntil
, takeWhile
, and unique
.
Each higher order message can be accessed as a dynamic property on a collection instance. For
instance, let’s use the each
higher order message to call a method on each object
within a collection:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">votes</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #89DDFF;">->each-></span><span style="color: #82AAFF;">markAsVip</span><span style="color: #BFC7D5;">();</span></div>
Likewise, we can use the sum
higher order message to gather the total number of
«votes» for a collection of users:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">group</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Development</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$users</span><span style="color: #89DDFF;">->sum->votes</span><span style="color: #BFC7D5;">;</span></div>
Before learning more about Laravel’s lazy collections, take some time to familiarize
yourself with PHP
generators.
To supplement the already powerful Collection
class, the LazyCollection
class leverages PHP’s generators to
allow you to work with very large datasets while keeping memory usage low.
For example, imagine your application needs to process a multi-gigabyte log file while taking
advantage of Laravel’s collection methods to parse the logs. Instead of reading the entire file
into memory at once, lazy collections may be used to keep only a small part of the file in
memory at a given time:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">LogEntry</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">make</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$handle</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fopen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">log.txt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">r</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">while</span><span style="color: #BFC7D5;"> ((</span><span style="color: #BEC5D4;">$line</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fgets</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">handle</span><span style="color: #BFC7D5;">)) </span><span style="color: #C792EA;">!==</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">yield</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$line</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;"> }</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunk</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$lines</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">LogEntry</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fromLines</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$lines</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">LogEntry</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$logEntry</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Process the log entry...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
Or, imagine you need to iterate through 10,000 Eloquent model
s. When using
traditional Laravel collections, all 10,000 Eloquent model
s must be loaded into
memory at the same time:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
However, the query builder’s cursor
method returns a LazyCollection
instance. This allows you to still only run a single query against the database
but
also only keep one Eloquent model
loaded in memory at a time. In this example, the
filter
callback is not executed until we actually iterate over each user
individually, allowing for a drastic reduction in memory usage:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">foreach</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">echo</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>
To create a lazy collection instance, you should pass a PHP generator function to the
collection’s make
method:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">make</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$handle</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fopen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">log.txt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">r</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">while</span><span style="color: #BFC7D5;"> ((</span><span style="color: #BEC5D4;">$line</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fgets</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">handle</span><span style="color: #BFC7D5;">)) </span><span style="color: #C792EA;">!==</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">yield</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$line</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;"> }</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
Almost all methods available on the Collection
class are also available on the
LazyCollection
class. Both of these classes implement the
Illuminate\Support\Enumerable
contract, which defines the following methods:
Methods that mutate the collection (such as shift
, pop
,
prepend
etc.) are not available on the
LazyCollection
class.
In addition to the methods defined in the Enumerable
contract, the
LazyCollection
class contains the following methods:
The takeUntilTimeout
method returns a new lazy collection that will enumerate values
until the specified time. After that time, the collection will then stop enumerating:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">times</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">INF</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntilTimeout</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">now</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addMinute</span><span style="color: #BFC7D5;">());</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$number</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">dump</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">number</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">sleep</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 58</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 59</span></div>
To illustrate the usage of this method, imagine an application that submits invoices from the
database
using a cursor. You could define a scheduled
task that runs every 15 minutes and only processes invoices for a maximum of 14 minutes:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Invoice</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Carbon</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Invoice</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pending</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntilTimeout</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Carbon</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">createFromTimestamp</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">LARAVEL_START</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">add</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">14</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">minutes</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> )</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">submit</span><span style="color: #BFC7D5;">());</span></div>
While the each
method calls the given callback for each item in the collection right
away, the tapEach
method only calls the given callback as the items are being
pulled out of the list one by one:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Nothing has been dumped so far...</span></div><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">times</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">INF</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">tapEach</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">dump</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Three items are dumped...</span></div><div class="line"><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">take</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 3</span></div>
The throttle
method will throttle the lazy collection such that each value is
returned after the specified number of seconds. This method is especially useful for situations
where you may be interacting with external APIs that rate limit incoming requests:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">vip</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">throttle</span><span style="color: #BFC7D5;">(seconds: </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Call external API...</span></div><div class="line"><span style="color: #BFC7D5;"> });</span></div>
The remember
method returns a new lazy collection that will remember any values that
have already been enumerated and will not retrieve them again on subsequent collection
enumerations:
<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> No query has been executed yet...</span></div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">remember</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> The query is executed...</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> The first 5 users are hydrated from the <code>database
...$users->take(5)->all();
// First 5 users come from the collection's cache...
// The rest are hydrated from the database
...
$users->take(20)->all();