Search

Laravel MessageBag Public API: jsonSerialize

November 29, 2016 —John Koster

The jsonSeralize method internally returns the value of the toArray method. This method exists to implement PHP's JsonSerializable interface, which allows developers to customize how a class is represented when using the json_encode function.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection.
6$collection = new Collection([
7 'first', 'second', 'third'
8]);
9 
10// Get the value that should be encoded with
11// json_encode.
12$value = $collection->jsonSerialize();

The $value variable would contain a value similar to the following output:

1array (size=3)
2 0 => string 'first' (length=5)
3 1 => string 'second' (length=6)
4 2 => string 'third' (length=5)

#Continue Reading

This article is the start of a mini-series about Laravel's ErrorMessageBag component. Click through the rest of the articles to continue reading:

Some absolutely amazing
people

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.