Skip to content
Prev Previous commit
Next Next commit
Add toPrettyJson to Fluent
  • Loading branch information
WendellAdriel committed Aug 21, 2025
commit d59eb4a4459f52874e0fbcf980ddd531871d6b06
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ public function toJson($options = 0)
return json_encode($this->jsonSerialize(), $options);
}

/**
* Convert the fluent instance to a pretty print formatted JSON.
*
* @return string
*/
public function toPrettyJson()
{
return $this->toJson(JSON_PRETTY_PRINT);
}

/**
* Determine if the fluent instance is empty.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportFluentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ public function testToJsonEncodesTheToArrayResult()
$this->assertJsonStringEqualsJsonString(json_encode(['foo']), $results);
}

public function testToPrettyJson()
{
$fluent = $this->getMockBuilder(Fluent::class)->onlyMethods(['toArray'])->getMock();
$fluent->expects($this->exactly(2))->method('toArray')->willReturn(['foo' => 'bar', 'bar' => 'foo']);
$results = $fluent->toPrettyJson();
$expected = $fluent->toJson(JSON_PRETTY_PRINT);

$this->assertJsonStringEqualsJsonString($expected, $results);
$this->assertSame($expected, $results);
$this->assertStringContainsString("\n", $results);
$this->assertStringContainsString(' ', $results);
}

public function testScope()
{
$fluent = new Fluent(['user' => ['name' => 'taylor']]);
Expand Down