Search

Laravel 5 Collections: Testing All Collection Values With every

April 22, 2018 —John Koster

The every method is used to determine if each element of the collection passes a given truth test. The every method can accept either a callback function to perform the truth test, or an operator and comparison value.

#Signature

1public function every(
2 $key,
3 $operator = null,
4 $value = null
5);

#Example Use

The following examples will demonstrate the various ways to invoke the every method:

1// Determine if all input characters are alphabetic.
2$allAlphabetic = collect(['a', 'b', 2, 3, 6, '%'])
3 ->every(function ($char) {
4 return ctype_alpha($char);
5 });

After the above code has executed, the $allAlphabetic variable would contain the value false. In the following example, we will check to see if all users in the collection are verified or not (we will also use strict comparison by supplying the === operator):

1$users = collect([
2 [
3 'username' => 'Alice',
4 'verified' => true
5 ],
6 [
7 'username' => 'Bob',
8 'verified' => true
9 ],
10 [
11 'username' => 'Charlie',
12 'verified' => true
13 ]
14]);
15 
16// Are all users verified?
17$allVerified = $users->every('verified', '===', true);

The following table lists all of the valid operators that can be used when invoking the every method:

Operator Description
= or == Equality operator. Returns true if the left hand side value equals the right hand side value.
!= or <> Not equal operator. Returns true if the left hand side value does not equal the right hand side value.
< Less than operator. Returns true if the left hand side value is less than the right hand side value.
> Greater than operator. Returns true if the left hand side value is greater than the right hand side value.
<= Less than or equal to operator. Returns true if the left hand side value is less than or equal to the right hand side value.
>= Greater than or equal to operator. Returns true if the left hand side value is greater than or equal to the right hand side value.
=== Identity operator. Returns true if the left hand type and value match the right hand side type and value.
!== Not identical operator. Returns true if the left hand type or value do not match the right hand side type or value.

#Using every With Higher Order Messages

The every method can also be used with higher order messaging, which allows us to invoke collection methods and access properties on objects using PHP's property accessors syntax. In the following example, we will create a color class with a method to indicate whether or not the color is a primary color:

1class Color
2{
3 /**
4 * The name of the color.
5 *
6 * @var string
7 */
8 protected $name = '';
9 
10 /**
11 * Indicates if the color is a primary color or not.
12 *
13 * @var bool
14 */
15 protected $isPrimary = false;
16 
17 public function __construct($name, $isPrimary)
18 {
19 $this->name = $name;
20 $this->isPrimary = $isPrimary;
21 }
22 
23 /**
24 * Indicates if the color is a primary color or not.
25 *
26 * @return bool
27 */
28 public function isPrimary()
29 {
30 return $this->isPrimary;
31 }
32}

The following example will populate a collection with colors and then determine if each color is a primary color:

1$colors = collect([
2 [
3 'name' => 'red',
4 'primary' => true
5 ],
6 [
7 'name' => 'yellow',
8 'primary' => true
9 ],
10 [
11 'name' => 'blue',
12 'primary' => true
13 ]
14])->transform(function ($item) {
15 return new Color(
16 $item['name'],
17 $item['primary']
18 );
19});
20 
21// Are all the colors primary?
22$allPrimary = $colors->every->isPrimary();

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.