Search

The blog - deep space.

Laravel Collection Public API: slice

Nov 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 Collection Public API: sort

Nov 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 Collection Public API: sortBy

Nov 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 Collection Public API: sortByDesc

Nov 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 Collection Public API: splice

Nov 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 Collection Public API: sum

Nov 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 Collection Public API: take

Nov 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.
Laravel Collection Public API: toJson

Nov 30, 2016

The toJson method in Laravel allows you to easily convert a collection into its JSON representation. By default, it uses PHP's json_encode function and can accept optional $options for encoding. You can also format the JSON output by passing in the JSON_PRETTY_PRINT constant. If you have deeply nested data structures and need to specify a higher depth for encoding, you can accomplish this by using json_encode directly on the jsonSerialize method of the collection instance.
Laravel Collection Public API: transform

Nov 30, 2016

The transform method in Laravel is similar to the map method, but instead of creating a new Collection instance, it modifies the original one. This method is useful when you want to make changes to each item in the collection. In the provided code example, the transform method is used to convert all the strings in the collection to uppercase.
Laravel Collection Public API: unique

Nov 30, 2016

The unique method in Laravel allows you to get all the unique items in a collection. By default, it performs a case-sensitive check, but you can provide a $key option to further restrict which items are returned. The method returns a new instance of Collection with the unique items. You can also use a callback function as the $key to perform custom uniqueness checks, like case-insensitive checks.
Laravel Collection Public API: values

Nov 30, 2016

The values method in Laravel can be used to extract only the values from a collection. It creates a new collection with consecutive numerical keys. Here is an example of how to use the values method in PHP. After executing the code, the resulting collection will contain the values without the original keys.
Laravel Collection Public API: where

Nov 30, 2016

The where method in Laravel allows developers to filter a collection based on a key-value pair. It returns a new collection with items that match the given criteria. The method supports various operators such as equality, inequality, and comparisons. In older versions of Laravel, the filter method can be used for similar functionality.
Laravel Collection Public API: whereIn

Nov 30, 2016

The whereIn method filters a collection based on a specific key and an array of possible values for that key. By default, it checks the types of the values in the collection against the types in the given array. The method returns a new, modified collection without changing the original instance. To use whereIn, you simply call it on a collection and pass the desired key and values as arguments, optionally specifying $strict to control type-checking behavior.
Laravel Collection Public API: whereInLoose

Nov 30, 2016

The whereInLoose method in Laravel's collection allows you to filter the collection based on a given key and an array of possible values, without performing type checking. It is similar to the whereIn method, but does not require a third argument. This method returns a new modified Collection instance, without changing the original instance.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.