The Blog

Laravel 5

Laravel 5

Laravel 5 Collections: Filtering a Collection Based On Key Presence Using Strict Comparison Operators With whereInStrict

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

Laravel 5 Collections: Filtering a Collection Based On Key Presence Using Strict Comparison Operators With whereNotInStrict

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

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.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more