How to display a readable array - Laravel

How to display a readable array - Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to display a readable array in Laravel

When you run dd(User::all()) in a route, Laravel's dump() and dd() helpers invoke var_dump(), which outputs a raw, unformatted text representation of the data. For a single object or a small scalar value, this is sufficient. However, when dealing with collections containing dozens or hundreds of users, the output becomes an unreadable wall of text where property names and values are indistinguishable without significant manual parsing.

To render this data in a readable format within the browser, you should stop relying on dd() for final user-facing responses and instead leverage Laravel's built-in JSON collection methods. The most direct approach is to convert the collection into a formatted string using toJson(). However, the default JSON output might still lack the visual spacing required to make complex structures easy to scan. You can solve this by adding the pretty flag to the method call.

Route::get('/', function() {
    $users = User::all();
    
    // Returns a nicely indented JSON string
    return response()->json($users->toJson(PRETTY_PRINT));
});

By passing JSON_PRETTY_PRINT (or the constant value 128) as the second argument to toJson(), Laravel formats the output with indentation and line breaks. This transforms the dense block of text into a structured list that is immediately parseable by a human reader in the browser console or network tab.

If you are working with older versions of PHP or specific environments where constants might behave differently, you can explicitly pass the numeric value 128 to ensure compatibility. It is important to note that this approach returns a string response suitable for debugging views or API endpoints, rather than terminating execution like dd(). If your goal is strictly for debugging purposes and you still want to halt execution after logging the formatted output, you can combine dump() with the formatted string:

Route::get('/', function() {
    $formattedUsers = User::all()->toJson(PRETTY_PRINT);
    
    // Outputs the readable JSON to the screen but does not stop execution
    dump($formattedUsers);
});

Alternatively, if you are debugging within a view blade file or need to inspect the data during development without altering the response structure, you can use dd() on the collection directly. While this still results in a raw var_dump output by default, modern IDEs and browser console integrations often handle large arrays better than the legacy CLI-style dump. However, for true readability in the HTTP response body, converting to pretty-printed JSON is the standard best practice.

When dealing with very large datasets, consider the performance implications of rendering full object graphs as JSON. If User::all() returns millions of records, serializing them into a string will consume significant memory and increase response time. In production environments, it is often better to limit the query scope using take() or specific filters before applying the formatting logic.

Another common pitfall involves accessing nested relationships. When you dump a User model with its associated posts, the resulting JSON includes all relationship data by default. While this aids debugging, it can bloat the output unnecessarily if you only need to inspect the primary entity's attributes. You might choose to hydrate or access specific properties manually before serialization:

$user = User::find(1);
$output = [
    'id' => $user->id,
    'name' => $user->name,
    'email' => $user->email,
];

return response()->json($output, 200, ['Content-Type' => 'application/json']);

This manual construction ensures that the JSON remains clean and focused on the relevant data points. For further reading on handling collections and responses effectively, resources like https://laravelcompany.com offer in-depth guides on building robust API endpoints.

Our network