In Laravel 5

Laravel 5 Collections: Filtering a Collection Based On Key Presence With whereIn

Apr 22, 2018

The whereIn method allows you to filter a collection based on a specific key and an array of possible values for that key. By default, it also checks the types of the values in the collection against the types in the supplied array. This method does not modify the original collection and returns a new, modified collection. You can use the whereIn method to quickly retrieve a subset of a collection based on a specific key-value pair.
Laravel 5 Collections: Filtering a Collection Based On Key Presence With whereNotIn

Apr 22, 2018

The whereNotIn method filters a collection to only include items that do not have matching values for a specified key. This method can be used with both arrays and objects. It does not modify the original collection. In an example use case, you can easily retrieve a list of users that are not banned from an online community by using the whereNotIn method with a list of banned users. By default, this method does not perform strict comparisons, but you can enable strict comparisons when necessary.
Laravel 5 Collections: Filtering Collection Elements Using Strict Comparison Operators With whereStrict

Apr 22, 2018

The whereStrict method in Laravel is used to perform strict comparisons when filtering results. It is similar to the where method, but always uses strict comparisons. For example, if you have a collection with a price represented as a float and another as a string, using whereStrict('price', 499.99) will only return the product with the float price, whereas where('price', '===', 499.99, true) will behave the same way.
Laravel 5 Collections: Filtering Collection Elements With filter

Apr 22, 2018

The filter method allows you to apply a filter to a collection, removing any values that evaluate to false, or based on a custom truth test provided with a callback. This method does not modify the original collection and returns a modified copy. You can also use the filter method with higher order messaging to access object instance properties and invoke collection methods.
Laravel 5 Collections: Filtering Collection Elements With where

Apr 22, 2018

The where method in Laravel allows developers to filter a collection based on a key value pair. By providing the key, operator, and value, the method checks that the key has a value equal to the provided value. The method returns a new collection with items that match the given criteria, without modifying the original collection. The where method supports different operators such as equality, inequality, and comparison operators. In Laravel 5.2 and below, similar functionality can be achieved using the filter method.
Laravel 5 Collections: Filtering Collections Based on Key Presence With intersectByKeys

Apr 22, 2018

Learn how to use the intersectByKeys method in Laravel to get a new collection that contains elements not present in an array or another collection. This method is similar to PHP's array_intersect_key function. Check out an example usage to understand how it works.
Laravel 5 Collections: Filtering Collections Based On Value Presence With intersect

Apr 22, 2018

The intersect method in Laravel's Collection class removes any values that are not in the provided array, returning a new collection instance. Keys from the original collection are preserved. Use this method when you want to filter a collection based on another array of values. The behavior of the intersect method is similar to PHP's array_intersect function. Check out the code example to see how it is used.
Laravel 5 Collections: Filtering Collections With (Without Losing Filtered Rejections) partition

Apr 22, 2018

The partition method in PHP allows you to create two subsets of a collection based on a truth condition. Unlike the filter method, the partition method does not discard elements that failed the truth condition. Instead, it returns an array of two collections. This method is particularly useful when combined with PHP's list function. You can use the partition method with higher order messaging to access object instance properties and invoke collection methods.
Laravel 5 Collections: Finding an Element Key Conditionally With search

Apr 22, 2018

The search method is used to search a collection for a given value or a callback function. If the value is found, the corresponding key is returned, otherwise it returns false. By default, the search is not strict and does not check for data types. However, you can enable strict mode to ensure data type matches. Additionally, you can provide a custom comparison function for more complex searches.
Laravel 5 Collections: Getting the First Collection Element With first

Apr 22, 2018

The first method in Laravel's Collection class is used to retrieve the first item in a collection. It can also be used with a callback function to find the first item that matches certain criteria. If the collection is empty, it will return null, unless a default value is provided. The first method can also be used with higher order messaging to access object instance properties. Example use cases include retrieving the first item in a collection, handling empty collections, using a callback to filter the collection, and using higher order messaging to retrieve specific items.
Laravel 5 Collections: Getting the Last Collection Element With last

Apr 22, 2018

The last method in Laravel's Collection class is used to retrieve the last item in a collection or the last item that matches a given condition. You can pass a callback function to define the condition for matching. If the collection is empty, the method will return null, unless a default value is provided.
Laravel 5 Collections: Getting the Number of Elements in a Collection With count

Apr 22, 2018

The count method in the Laravel framework returns the number of elements in the collection. It can be used to count the number of items in a collection and implements PHP's Countable interface. This method is synonymous with PHP's count function and returns an integer value.
Laravel 5 Collections: Grouping Collection Elements With groupBy

Apr 22, 2018

The groupBy method in Laravel is used to group a collection based on a given value. The value can be specified as a simple string or a callback function. By default, keys are not preserved during grouping, but this can be changed by passing true as an argument. The method returns a new instance of the Collection class. Examples are provided to demonstrate how groupBy can be used with different parameters.
Laravel 5 Collections: Mapping Collection Elements and Reducing to a Single Dimension With flatMap

Apr 22, 2018

The flatMap method is used to collapse a collection into a new modified collection. It does not modify the original collection, but returns a new one with the modified elements. You can use flatMap to apply higher order messaging and transform nested objects into arrays. This is useful when you need to extract specific values or modify the structure of your collection.

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.