Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() with Axios Vue and Laravel 7

Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() with Axios Vue and Laravel 7

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

Stefan Bogdanescu

Founder & Senior Architect

Argument 1 Passed to Symfony\Component\HttpFoundation\Response::setContent() with Axios Vue and Laravel 7

You are encountering a classic PHP type error that stems from a mismatch between what your backend expects to return and how Laravel handles API responses by default. The stack trace Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given reveals that a controller (or closure route) is attempting to return an Eloquent model collection directly, rather than formatting it as JSON.

In your specific setup using Laravel 7, Vue, and Axios, the issue lies within your api.php routes file. Specifically, look at these two lines:

Route::get('contacts', function () {
    return Contact::latest()->orderBy('created_at', 'desc');
});

Route::get('contact/{id}', function ($id) {
    return Contact::findOrFail($id);
});

Laravel's default behavior is to inspect the return statement of a route. If you return a model instance or an array, Laravel will automatically serialize it into JSON using its built-in API response mechanism. However, in older versions of Symfony and specific configurations within Laravel 7, if the underlying framework expects a string content for a direct Response object creation but receives an object (like a raw Eloquent collection), it throws this specific exception before the serialization middleware can fully intervene, or because the route is being treated as returning a non-API response.

The error occurs because you are returning Contact::latest() directly. While modern Laravel versions usually handle this gracefully by wrapping it in response()->json(), explicit type enforcement in the underlying Symfony HTTP layer requires the content to be a string when constructing specific response objects manually, or it expects the standard JSON Response object returned by Response::make().

To resolve this without relying on implicit behavior that might change between minor versions, you should explicitly tell Laravel to return a JSON response. You have two primary options.

Explicitly Return JSON

The most robust fix is to wrap your data in response()->json(). This ensures that the content type header is set correctly and that the object is serialized into a string before being sent over the wire.

Update your api.php routes as follows:

use App\Models\Contact;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('contacts', function () {
    // Explicitly return JSON to avoid type errors
    return response()->json(Contact::latest()->orderBy('created_at', 'desc'));
});

Route::get('contact/{id}', function ($id) {
    return response()->json(Contact::findOrFail($id));
});

Using Route Macros or Controllers

If you prefer to keep your route closures clean, you can define a macro in AppServiceProvider to handle Eloquent collections automatically. However, for Laravel 7, the explicit response()->json() approach is safer and more predictable.

Alternatively, migrating these closures to a dedicated Controller class allows you to leverage Laravel's native response helpers more cleanly:

// In your ContactController.php
public function index()
{
    return json_response(Contact::latest()->orderBy('created_at', 'desc'));
}

Once you make this change, restart your PHP server. The backend will now correctly serialize the collection into a JSON string (e.g., [{ "id": 1, "name": "...", ... }]) rather than trying to pass an object instance directly to the Symfony response layer.

Your Vue component and Axios calls do not need any changes. The response.data property in your .then() block will continue to contain the parsed JSON object exactly as expected. By ensuring the backend returns a proper JSON Response object, you align with the standard REST API contract and eliminate the risk of Symfony throwing type mismatch exceptions during content setting.

Our network