Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given

Updated at 24-Aug-2021, By samar

Argument 1 passed to SymfonyComponentHttpFoundationResponse::setContent() must be of the type string or null, object given

We will use programming in this lesson to attempt to solve the "Argument 1 passed to SymfonyComponentHttpFoundationResponse::setContent() must be of the type string or null, object given".

Sometimes you got error Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given because you return object directly instead of passing to view or return in json format.
  • Return data to view or in json format instead of return object

    $lastUser = DB::table('users')->latest()->first();
    //return $lastUser;
    return response()->json($lastUser);
    //Or
    return view('home', compact('lastUser'));
    

    If you are getting error Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, just remove return $lastUser from code or just replace code with return view('home', compact('lastUser')); or you can also return by calling return response()->json($lastUser); method. To return response the content should be type of string or null in our case.

Back to code snippet queries related laravel