The Blog

Laravel

Laravel

Laravel Collection Public API: search

John Koster
John Koster
November 30, 2016

The search method is used to search for a given $value within the collection. When found, it returns the corresponding key, otherwise it returns false. You can also use the $strict parameter to check for exact data types in the collection. You can pass either a value or a callback function as the $value argument. Code examples are provided to illustrate the usage of the search method.

Laravel

Laravel Collection Public API: shift

John Koster
John Koster
November 30, 2016

The shift method in PHP's Illuminate\Support\Collection class is used to remove the first item from the collection and return its value. It modifies the original collection instance. In the provided example, the shift method is used to remove the first item 'first' from the collection, resulting in the collection to only contain 'second' and 'third'.

Laravel

Laravel Collection Public API: shuffle

John Koster
John Koster
November 30, 2016

Learn about the shuffle method in Laravel, which rearranges an array in a random distribution. This method returns a new collection without modifying the original instance. Use the provided code example to understand how shuffle can be implemented. Remember that the output will change each time the code is executed, so the given output is just an example.

Laravel

Laravel Collection Public API: slice

John Koster
John Koster
November 30, 2016

The slice method in Laravel's Illuminate\Support\Collection class is used to retrieve a slice or portion of a collection. It takes an $offset parameter to determine where to start the slice. If the $offset is negative, the slice will start from the end of the collection. The slice method returns a new instance of Collection, and the original collection remains unchanged. The returned collection can be further controlled using the optional $length parameter, which determines the number of items in the slice. Additionally, developers can preserve the keys of the original collection by passing true as the third parameter in the slice method call.

Laravel

Laravel Collection Public API: sort

John Koster
John Koster
November 30, 2016

The sort method is used to sort a collection in a specific order. If no custom sorting comparison function is provided, the collection will be sorted in a case-insensitive "natural order". A custom sorting comparison function can be supplied using the $callback parameter. The sort method returns a new instance of Collection and does not modify the original collection. The example demonstrates how to use the sort method with and without a custom sorting comparison function.

Laravel

Laravel Collection Public API: sortBy

John Koster
John Koster
November 30, 2016

Learn how to use the sortBy method in Laravel's Collection class to sort collection items based on a callback function or by a specific key. Sort the collection in either ascending or descending order. The sortBy method does not modify the original collection but returns a new sorted instance. Find code examples to sort a collection by name or by the number of followers. Additionally, understand the different sorting options available, such as SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, and SORT_FLAG_CASE. Get a comparison table showing the new collection order based on each sorting option.

Laravel

Laravel Collection Public API: sortByDesc

John Koster
John Koster
November 30, 2016

The sortByDesc method in Laravel operates the same as the sortBy method, but without the $descending parameter. Internally, sortByDesc calls sortBy with true as the $descending parameter. The following examples demonstrate how to use sortBy and sortByDesc to sort a collection by the name attribute.

Laravel

Laravel Collection Public API: splice

John Koster
John Koster
November 30, 2016

The splice method in Laravel is used to remove a portion of a collection and return the removed section. It takes three parameters: $offset, $length, and $replacement. The $offset parameter determines where to begin removing items from the collection. The method modifies the original Collection instance and returns the removed items as a new Collection instance. You can also specify a replacement array to replace the removed items in the collection.

Laravel

Laravel Collection Public API: sum

John Koster
John Koster
November 30, 2016

The sum method is used to calculate the sum of all items in a collection. It takes an optional $callback parameter which can be used to specify which values should be added together. If the collection is empty, the method will return 0. In the given examples, the sum method is used to calculate the total number of speakers at Laracon EU for different years. The total can be counted by passing a callback function as the argument to the $callback parameter.

Laravel

Laravel Collection Public API: take

John Koster
John Koster
November 30, 2016

The take method in Laravel is used to retrieve a specified number of items from a collection. It can retrieve items from the beginning or end of the collection depending on the provided $limit. The method returns a new Collection instance, leaving the original collection unchanged. This allows for easy manipulation and retrieval of data from collections in Laravel applications.

Latest posts

Where Things get Good: Moving on to Forte Development Phase 1

Wrapping up Forte Phase 0 with the lexer and parser now in private alpha, and kicking off Phase 1 wi...

Read more
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