From c9f0d051992c3c6e4d07831a4708b6d11e845bca Mon Sep 17 00:00:00 2001 From: Sylvain WITMEYER Date: Fri, 28 Oct 2016 22:27:52 -0400 Subject: [PATCH] Model::toJson() silently fails on error I had not UTF-8 chars into a legacy database and this method was silently failing. I think it's a good impovement to throw an InvalidArgumentException --- src/Illuminate/Database/Eloquent/Model.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 5fe4bb82da82..802e59f17f35 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2396,10 +2396,18 @@ public function setIncrementing($value) * * @param int $options * @return string + * + * @throws \InvalidArgumentException */ public function toJson($options = 0) { - return json_encode($this->jsonSerialize(), $options); + $json = json_encode($this->jsonSerialize(), $options); + + if (JSON_ERROR_NONE !== json_last_error()) { + throw new InvalidArgumentException(json_last_error_msg()); + } + + return $json; } /**