The blog

Laravel 5 Collections: Executing a Function on Collection Elements and Values With mapSpread

Apr 22, 2018

The mapSpread method in PHP is used to execute a callback on a collection, allowing you to work with nested collections or Traversable objects. This method returns a new collection without modifying the original one. You can use mapSpread to replicate the behavior of LINQ's Zip operator, which combines two collections and applies a function to create a new collection.
Laravel 5 Collections: Executing a Function on Collection Elements With eachSpread

Apr 22, 2018

The eachSpread method allows you to execute a callback function on each element in a collection, using the element's values as arguments. You can stop iterating the collection by returning false from the callback function.
Laravel 5 Collections: Filtering a Collection Based On Key Presence Using Strict Comparison Operators With whereInStrict

Apr 22, 2018

The whereInStrict method is used to determine the collection elements that match a key-value pair using strict comparison. This method returns a new collection without modifying the original one. For example, if we have a collection of people with different age values including integers and strings, we can use whereInStrict or whereIn with strict mode to retrieve elements based on a specific age value.
Laravel 5 Collections: Filtering a Collection Based On Key Presence Using Strict Comparison Operators With whereNotInStrict

Apr 22, 2018

The whereNotInStrict method filters a collection to return all items that do not have matching values for a given key. This method can be used with arrays and objects alike, and it performs strict comparisons. The original collection is not modified. An example use case is filtering a collection of users to only include those who are not banned.
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.

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.