The Blog

Laravel 5

Laravel 5

Laravel 5 Collections: Retrieving Random Collection Elements With random

John Koster
John Koster
April 22, 2018

The random method in Laravel allows you to retrieve a random number of items from a collection. By default, it returns only one item, but you can specify a different number. If multiple items are requested, a Collection instance is returned. If the requested number exceeds the total items in the collection, an InvalidArgumentException is thrown. Here's an example of how to use the random method in Laravel.

Laravel 5

Laravel 5 Collections: Retrieving the Collection Element's Keys With keys

John Koster
John Koster
April 22, 2018

The keys method is used to retrieve the keys of all items in a collection. It returns a new Collection instance with the keys as its items. The behavior of the keys method is similar to PHP's array_keys function. You can use it to retrieve the keys from a collection, whether it contains indexed or associative arrays.

Laravel 5

Laravel 5 Collections: Retrieving the Distinct Values of a Collection Using Strict Comparison Operators With uniqueStrict

John Koster
John Koster
April 22, 2018

The uniqueStrict method in Laravel's Collection class allows you to retrieve all unique items in a collection using strict comparisons. It is similar to the unique method, but it only considers strict comparisons when determining uniqueness. You can also provide a key argument to restrict the conditional check.

Laravel 5

Laravel 5 Collections: Retrieving the Distinct Values of a Collection With unique

John Koster
John Koster
April 22, 2018

The unique method in Laravel is used to obtain all the distinct items in a collection. It can be called with an optional argument to further filter the items returned. The method returns a new Collection object with the unique items. In a simple usage example, the method can be called on a collection to get all the unique items. The method is case-sensitive by default. It can also be invoked with a callback function to perform a case-insensitive check for uniqueness.

Laravel 5

Laravel 5 Collections: Retrieving the Maximum Value of a Collection With max

John Koster
John Koster
April 22, 2018

The max method in Laravel can be used to retrieve the maximum value of a given key. By default, the key is null and it behaves similarly to PHP's max function. In the first example, without specifying a key, the method returns the maximum value from the collection. In the second example, a key is specified and the method returns the maximum value based on that key.

Laravel 5

Laravel 5 Collections: Returning Collection Elements That Are Not Present in the Specified Collections With diffAssoc

John Koster
John Koster
April 22, 2018

The diffAssoc method in Laravel returns a new collection containing the items where the keys and values are not present in the provided arguments. It is similar to PHP's array_diff_assoc function. In the example usage, $newInformation will store any new information from the $contactInformation collection that is not found in the provided arguments.

Laravel 5

Laravel 5 Collections: Reversing the Order of Collection Elements With reverse

John Koster
John Koster
April 22, 2018

Learn about the reverse method in Laravel's Collection class. This method is used to reverse the order of items in a collection. It returns a new Collection instance that contains the reversed items. The numerical keys are not preserved, but non-numerical keys are preserved.

Laravel 5

Laravel 5 Collections: Sort a Collection in Descending Order With sortByDesc

John Koster
John Koster
April 22, 2018

The sortByDesc method is similar to the sortBy method, but it does not have a $descending parameter. Instead, it internally calls the sortBy method with true as the argument for the $descending parameter. Both methods allow you to sort a collection based on a callback function or a property. You can also use the sortByDesc method with higher order messages, allowing you to sort a collection in reverse order based on a property.

Laravel 5

Laravel 5 Collections: Sorting a Collection Based on a Key Value With sortBy

John Koster
John Koster
April 22, 2018

The sortBy method in Laravel allows you to sort a collection by a key or a value returned by a callback function. You can modify the sorting behavior using the $options parameter and choose whether to sort in ascending or descending order with the $descending parameter. The method returns a new instance of the Collection class with the items sorted and does not modify the original collection. Examples demonstrate sorting by name, number of followers, and using different sorting flags such as SORT_NUMERIC and SORT_STRING.

Laravel 5

Laravel 5 Collections: Sorting a Collection With sort

John Koster
John Koster
April 22, 2018

The sort method is used to sort a collection. By default, it uses a case-insensitive "natural order" algorithm. You can also provide a custom comparison function as an argument to the method. The original collection remains unchanged, and the method returns a new instance of Collection. The keys of the collection items are preserved during sorting. In the examples provided, the sort method is used to sort a collection of numbers in ascending order and a collection of people based on their age.

Latest posts

Forte Update: Backtracking, Metadata, HTML Validation, and More

A Forte development update: the parser now supports backtracking, improvements to node metadata, ide...

Read more
Parsing HTML and Blade Attributes in Forte

Wrapping up attribute parsing in Forte's HTML parser, from simple HTML attributes to complex, edge-c...

Read more
Switch Statements and Parser Extensions in Forte

Exploring how Forte's parser extensions can be used to handle complex Blade directives like nested s...

Read more
Parsing Blade Comments in Forte

Digging into parsing Blade and HTML comments while building Forte's HTML parser for Laravel Blade.

Read more
Thoughts on HTML Elements and Blade Components in Forte

This week I’m tackling Forte's HTML parser - consolidating Blade, Flux, and Livewire components into...

Read more
Laracon, Blade and What's Next

Recapping Laracon in Denver with the Statamic team, this post introduces Forte - a new Blade extensi...

Read more