April 22, 2018 —John Koster
The jsonSerialize
method will convert the contents of the collection into something that be serialized using PHP's json_encode
function. This method will iterate the collection's internal array and will evaluate each element in accordance with the following rules:
JsonSerializable
, the results of the jsonSerialize
method will be returned;toJson
method will be returned;toArray
method will be returned;1public function jsonSerialize();
The following code example demonstrates the basic use of the jsonSerialize
method:
1use Illuminate\Support\Collection; 2 3// Create a new collection. 4$collection = new Collection([ 5 'first', 'second', 'third' 6]); 7 8// Get the value that should be encoded with 9// json_encode.10$value = $collection->jsonSerialize();
The $value
variable would contain a value similar to the following output:
1array2 0 => string 'first'3 1 => string 'second'4 2 => string 'third'
∎
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.