April 22, 2018 —John Koster
The reject
method used to create a new Collection
instance containing all the items in the collection that do not pass a given truth test. The reject
method only defines one parameter: $callback
. The $callback
function should only have one parameter $item
, which will contain the collection item under test. The $callback
should return true
if the item should not be included in the final collection.
To put it another way, the $callback
function answers the question "Should I remove this item from the collection?"; returning true
from the callback function means it will be removed while false
indicates that it should remain in the collection.
The reject
method does not modify the original collection instance and will return a new collection with the limited results.
1public function reject(2 $callback3);
The following code example demonstrates the usage of the reject
method by rejecting any name that does not begin with the letter a
(ignoring case):
1use Illuminate\Support\Collection; 2use Illuminate\Support\Str; 3 4// Create a new collection instance. 5$collection = new Collection([ 6 'Alice', 'Anna', 'Bob', 'Bev' 7]); 8 9// Only keep names that begin with the letter 'a'.10$names = $collection->reject(function($item), {11 return !Str::startsWith(Str::lower($item), 'a');12});
After the above code executes, the $names
variable will be an instance of Collection
and contain a value similar to the following output:
1object(Illuminate\Support\Collection)2 protected 'items' =>3 array4 0 => string 'Alice'5 1 => string 'Anna'
reject
With Higher Order MessagesThe reject
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 simple Product
class which will contain the name of a product, its price, and whether it is on sale or not.
The Product
Class:
1class Product 2{ 3 public $name = ''; 4 public $price = 0.0; 5 public $onSale = false; 6 7 /** 8 * Determines if the product is on sale. 9 *10 * @return bool11 */12 public function isOnSale() {13 return $this->onSale === true;14 }15 16 /**17 * Determines if the product is not on sale.18 *19 * @return bool20 */21 public function isNotOnSale() {22 return !$this->isOnSale();23 }24}
Using the Product
class, we will now create a new collection containing product instances. We will then use higher order messages to reject all of the products that are not on sale:
1$products = collect([ 2 [ 3 'name' => 'Office Chair', 4 'price' => 399.99, 5 'onSale' => false 6 ], 7 [ 8 'name' => 'Desk', 9 'price' => 199.34,10 'onSale' => true11 ]12])->transform(function ($item) {13 $product = new Product;14 $product->name = $item['name'];15 $product->price = $item['price'];16 $product->onSale = $item['onSale'];17 18 return $product;19})->reject->isNotOnSale();
After the above code has executed, the $products
collection would contain only one item: the Desk
, since it is currently on sale.
∎
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.