Skip to content

Commit

Permalink
Include that toJson() can take an options parameter
Browse files Browse the repository at this point in the history
Add that you can pass in a bitmask of options to `toJson()` that gets passed to `json_encode`.

See also laravel/framework#3929
  • Loading branch information
jk3us authored Mar 7, 2018
1 parent c9964e3 commit a7f84ec
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions eloquent-serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ You may also convert entire [collections](/docs/{{version}}/eloquent-collections
<a name="serializing-to-json"></a>
### Serializing To JSON

To convert a model to JSON, you should use the `toJson` method. Like `toArray`, the `toJson` method is recursive, so all attributes and relations will be converted to JSON:
To convert a model to JSON, you should use the `toJson` method. Like `toArray`, the `toJson` method is recursive, so all attributes and relations will be converted to JSON. An optional bitmask of options can be passed in as documented for PHP's [json_encode](http://php.net/manual/en/function.json-encode.php) function:

$user = App\User::find(1);

return $user->toJson();
return $user->toJson(JSON_PRETTY_PRINT);

Alternatively, you may cast a model or collection to a string, which will automatically call the `toJson` method on the model or collection:

Expand Down

0 comments on commit a7f84ec

Please sign in to comment.