When I look at the Illuminate\Support\MessageBag
class methods, the class looks like this:
use JsonSerializable;
use Illuminate\Contracts\Support\Jsonable;
....
class MessageBag implements Jsonable, JsonSerializable...
/*
* Convert the object to its JSON representation.
*/
public function toJson ($options = 0) {
return json_encode($this->jsonSerialize(), $options);
}
/*
*Convert the object into something JSON serializable.
*/
public function jsonSerialize() {
return $this->toArray();
}
Please tell me, seniors, what is the difference between the toJson method and the jsonSerialize method? When will it be called implicitly?
Refer to the document: http://php.net/manual/zh/json...
Upload the code:
JsonSerializable itself specifically serves json_encode serialization, and toJson is just the Jsonable method of laravel.
In other words, when you use json_encode to serialize this object, the jsonSerialize method will be called.
And your toJson usually just encapsulates the json_encode function, just for semantics.
Like this?
I don’t know much about it, so I searched it for you:
http://www.cnblogs.com/gniele...
Thanks for the invitation!
Because I have never used larval, can you post the specific code of toJson method and jsonSerialize method