Laravel Collection – Using toJson() Method
Laravel Collections ( 11 Lessons )
Laravel Collections is one of the most powerful features of Laravel. Basically, collections are PHP arrays but it’s an Object Oriented approach to deal with PHP arrays.
see full series- Laravel Collections – Introduction to Collections
- Laravel Collections – make Method
- Laravel Collections – wrap() and unwrap() Methods
- Laravel Collections – map() Method
- Laravel Collections – times() Method
- Laravel Collections – collapse() Method
- Laravel Collections – contains() & containsStrict() Methods
- Laravel Collections – each() Method
- Laravel Collection – Using toJson() Method
- Laravel Collection – Filtering Collection Items using where()
- Laravel Collection – Using splice() Method to Remove Items
Laravel Collection provides an easy interface to work with the PHP array. Today, we will be looking at how we can use the toJson()
method to convert a Laravel Collection to JSON format.
The toJson() method will return the JSON encoded version of the data stored in a Laravel Collection. Under the hood Laravel, apply the native json_encode()
method of PHP. This method takes one optional argument $options
. Like the json_encode function, the $options
parameter is a bitmask of the predefined JSON constants.
Signature
The signature of toJson() method in the Illuminate/Support/Collection
class looks something like below.
/** * Get the collection of items as JSON. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); }
Example
In the below example, we can see how toJson()
method can be used.
// Create a new collection for our example $collection = new Collection([ ['name' => 'iPhone 11 Pro', 'price' => 999], ['name' => 'iPhone 11', 'price' => 799], ]); // Convert the above collection to JSON format $jsonValue = $collection->toJson();
After you execute the above code, you will find the below output for our $jsonValue
variable.
[{"name":"iPhone 11 Pro","price":999},{"name":"iPhone 11","price":799}]
To print the formatted value, we can pass the JSON_PRETTY_PRINT
contant as a option.
// Create a new collection for our example $collection = new Collection([ ['name' => 'iPhone 11 Pro', 'price' => 999], ['name' => 'iPhone 11', 'price' => 799], ]); // Convert the above collection to JSON format $jsonValue = $collection->toJson(JSON_PRETTY_PRINT);
Now if you print the value of $jsonValue
, you will get the output something like below.
[ { "name": "iPhone 11 Pro", "price": 999 }, { "name": "iPhone 11", "price": 799 } ]
toJson() and Deep Nested Data Structures
As I mentioned above that toJson() method makes call to native PHP function json_encode
. Unlike the PHP’s json_encode
method, the toJson()
method does not provide the way to specify the depth of the array to which data will be encoded which is by default set to 512.
To convert a collection instance into its JSON with depth greater than 512, you can take the following approach.
use Illuminate\Support\Collection; // Create a new collection instance. $collection = new Collection([]); // Replace 512 with the desired depth. $jsonValue = json_encode( $collection->jsonSerialize(), 0, 512 );