By John Koster
The except
method will return all the key/value pairs in the collection where the keys in the collection are not in the supplied $keys
array. Internally, this method makes a call to the Illuminate\\Support\\Arr:except($array, $keys)
helper function.
#Signature
1public function except(
2 $keys
3);
#Example Use
The following example demonstrates the usage of the except
method:
1// Create a new collection.
2$collection = collect([
3 'first_name' => 'John',
4 'last_name' => 'Doe',
5 'password' => 'some_password'
6]);
7
8// Retrieve all items from the collect
9// except for the password.
10$data = $collection->except(['password']);
After the above code has executed, the $data
variable would contain a value similar to the following output:
1Collection {
2 #items: array [
3 "first_name" => "John"
4 "last_name" => "Doe"
5 ]
6}
We can also combine the except
method with the toJson
method to easily remove sensitive data from information before sending it to an end user:
1$jsonData = $collection->except(['password'])->toJson();
After the above code has executed, the $jsonData
variable would hold a value similar to the following JSON string:
1"{"first_name":"John","last_name":"Doe"}"
∎