The Blog

Laravel 5

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

John Koster
John Koster
April 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

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

John Koster
John Koster
April 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

Laravel 5 Collections: Filtering Collection Elements Using Strict Comparison Operators With whereStrict

John Koster
John Koster
April 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

Laravel 5 Collections: Filtering Collection Elements With filter

John Koster
John Koster
April 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

Laravel 5 Collections: Filtering Collection Elements With where

John Koster
John Koster
April 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

Laravel 5 Collections: Filtering Collections Based on Key Presence With intersectByKeys

John Koster
John Koster
April 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

Laravel 5 Collections: Filtering Collections Based On Value Presence With intersect

John Koster
John Koster
April 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

Laravel 5 Collections: Filtering Collections With (Without Losing Filtered Rejections) partition

John Koster
John Koster
April 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

Laravel 5 Collections: Finding an Element Key Conditionally With search

John Koster
John Koster
April 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

Laravel 5 Collections: Getting the First Collection Element With first

John Koster
John Koster
April 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.

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