File tree Expand file tree Collapse file tree 16 files changed +209
-2
lines changed Expand file tree Collapse file tree 16 files changed +209
-2
lines changed Original file line number Diff line number Diff line change @@ -1269,6 +1269,13 @@ public function jsonSerialize(): mixed;
12691269 */
12701270 public function toJson ($ options = 0 );
12711271
1272+ /**
1273+ * Get the collection of items as pretty print formatted JSON.
1274+ *
1275+ * @return string
1276+ */
1277+ public function toPrettyJson ();
1278+
12721279 /**
12731280 * Get a CachingIterator instance.
12741281 *
Original file line number Diff line number Diff line change @@ -984,6 +984,16 @@ public function toJson($options = 0)
984984 return json_encode ($ this ->jsonSerialize (), $ options );
985985 }
986986
987+ /**
988+ * Get the collection of items as pretty print formatted JSON.
989+ *
990+ * @return string
991+ */
992+ public function toPrettyJson ()
993+ {
994+ return $ this ->toJson (JSON_PRETTY_PRINT );
995+ }
996+
987997 /**
988998 * Get a CachingIterator instance.
989999 *
Original file line number Diff line number Diff line change @@ -1800,6 +1800,18 @@ public function toJson($options = 0)
18001800 return $ json ;
18011801 }
18021802
1803+ /**
1804+ * Convert the model instance to pretty print formatted JSON.
1805+ *
1806+ * @return string
1807+ *
1808+ * @throws \Illuminate\Database\Eloquent\JsonEncodingException
1809+ */
1810+ public function toPrettyJson ()
1811+ {
1812+ return $ this ->toJson (JSON_PRETTY_PRINT );
1813+ }
1814+
18031815 /**
18041816 * Convert the object into something JSON serializable.
18051817 *
Original file line number Diff line number Diff line change @@ -135,7 +135,7 @@ public function toArray(Request $request)
135135 }
136136
137137 /**
138- * Convert the model instance to JSON.
138+ * Convert the resource to JSON.
139139 *
140140 * @param int $options
141141 * @return string
@@ -153,6 +153,18 @@ public function toJson($options = 0)
153153 return $ json ;
154154 }
155155
156+ /**
157+ * Convert the resource to pretty print formatted JSON.
158+ *
159+ * @return string
160+ *
161+ * @throws \Illuminate\Database\Eloquent\JsonEncodingException
162+ */
163+ public function toPrettyJson ()
164+ {
165+ return $ this ->toJson (JSON_PRETTY_PRINT );
166+ }
167+
156168 /**
157169 * Get any additional data that should be returned with the resource array.
158170 *
Original file line number Diff line number Diff line change @@ -180,4 +180,14 @@ public function toJson($options = 0)
180180 {
181181 return json_encode ($ this ->jsonSerialize (), $ options );
182182 }
183+
184+ /**
185+ * Convert the object to pretty print formatted JSON.
186+ *
187+ * @return string
188+ */
189+ public function toPrettyJson ()
190+ {
191+ return $ this ->toJson (JSON_PRETTY_PRINT );
192+ }
183193}
Original file line number Diff line number Diff line change @@ -242,4 +242,14 @@ public function toJson($options = 0)
242242 {
243243 return json_encode ($ this ->jsonSerialize (), $ options );
244244 }
245+
246+ /**
247+ * Convert the object to pretty print formatted JSON.
248+ *
249+ * @return string
250+ */
251+ public function toPrettyJson ()
252+ {
253+ return $ this ->toJson (JSON_PRETTY_PRINT );
254+ }
245255}
Original file line number Diff line number Diff line change @@ -185,4 +185,14 @@ public function toJson($options = 0)
185185 {
186186 return json_encode ($ this ->jsonSerialize (), $ options );
187187 }
188+
189+ /**
190+ * Convert the object to pretty print formatted JSON.
191+ *
192+ * @return string
193+ */
194+ public function toPrettyJson ()
195+ {
196+ return $ this ->toJson (JSON_PRETTY_PRINT );
197+ }
188198}
Original file line number Diff line number Diff line change @@ -203,6 +203,16 @@ public function toJson($options = 0)
203203 return json_encode ($ this ->jsonSerialize (), $ options );
204204 }
205205
206+ /**
207+ * Convert the fluent instance to pretty print formatted JSON.
208+ *
209+ * @return string
210+ */
211+ public function toPrettyJson ()
212+ {
213+ return $ this ->toJson (JSON_PRETTY_PRINT );
214+ }
215+
206216 /**
207217 * Determine if the fluent instance is empty.
208218 *
Original file line number Diff line number Diff line change @@ -431,6 +431,16 @@ public function toJson($options = 0)
431431 return json_encode ($ this ->jsonSerialize (), $ options );
432432 }
433433
434+ /**
435+ * Convert the object to pretty print formatted JSON.
436+ *
437+ * @return string
438+ */
439+ public function toPrettyJson ()
440+ {
441+ return $ this ->toJson (JSON_PRETTY_PRINT );
442+ }
443+
434444 /**
435445 * Convert the message bag to its string representation.
436446 *
Original file line number Diff line number Diff line change @@ -3414,6 +3414,18 @@ public function testModelToJsonSucceedsWithPriorErrors(): void
34143414 $ this ->assertSame ('{"name":"Mateus"} ' , $ user ->toJson (JSON_THROW_ON_ERROR ));
34153415 }
34163416
3417+ public function testModelToPrettyJson (): void
3418+ {
3419+ $ user = new EloquentModelStub (['name ' => 'Mateus ' , 'active ' => true ]);
3420+ $ results = $ user ->toPrettyJson ();
3421+ $ expected = $ user ->toJson (JSON_PRETTY_PRINT );
3422+
3423+ $ this ->assertJsonStringEqualsJsonString ($ expected , $ results );
3424+ $ this ->assertSame ($ expected , $ results );
3425+ $ this ->assertStringContainsString ("\n" , $ results );
3426+ $ this ->assertStringContainsString (' ' , $ results );
3427+ }
3428+
34173429 public function testFillableWithMutators ()
34183430 {
34193431 $ model = new EloquentModelWithMutators ;
You can’t perform that action at this time.
0 commit comments