To use the "reject" method in the Illuminate/Collections package to filter out elements from a collection, you can follow these steps:
use Illuminate\Support\Collection;
$collection = new Collection([1, 2, 3, 4, 5]);
$filtered = $collection->reject(function ($value, $key) {
return $value % 2 !== 0;
});
$filtered->dump();
This will output the filtered collection without the odd numbers:
array:2 [▼
0 => 2
3 => 4
]
Alternatively, you can also assign the filtered collection to a new variable:
$filtered = $collection->reject(function ($value, $key) {
return $value % 2 !== 0;
});
Now, the $filtered
variable will contain the filtered collection.
By using the "reject" method, you can easily filter out elements from a collection based on your custom logic.