Illuminate\Support\Collection::jsonSerialize PHP Method

jsonSerialize() public method

Convert the object into something JSON serializable.
public jsonSerialize ( ) : array
return array
    public function jsonSerialize()
    {
        return array_map(function ($value) {
            if ($value instanceof JsonSerializable) {
                return $value->jsonSerialize();
            } elseif ($value instanceof Jsonable) {
                return json_decode($value->toJson(), true);
            } elseif ($value instanceof Arrayable) {
                return $value->toArray();
            } else {
                return $value;
            }
        }, $this->items);
    }

Usage Example

Example #1
0
 public function testJsonSerialize()
 {
     $c = new Collection([new TestArrayableObject(), new TestJsonableObject(), new TestJsonSerializeObject(), 'baz']);
     $this->assertSame([['foo' => 'bar'], ['foo' => 'bar'], ['foo' => 'bar'], 'baz'], $c->jsonSerialize());
 }