By John Koster
__toString()
When a Collection
instance is cast into a string, its JSON representation is returned as the result. Internally this is accomplished by returning the results of the collection's toJson
(discussed in the Laravel Collection Public API: toJson article) method.
1<?php
2
3use Illuminate\Support\Collection;
4
5// Create a new collection instance.
6$collection = new Collection([
7 'name' => 'Flo'
8]);
9
10// Cast the collection to a string.
11$stringValue = (string) $collection;
After the above code has executed, the $stringValue
variable will contain the following value:
1{"name":"Flo"}
∎