Home > Mobile >  Response JSON error - Type is not supported when using Eloquent
Response JSON error - Type is not supported when using Eloquent

Time:10-08

I have read through the documentation and a few tutorials. Sadly, I can't seem to find any documentation on what is returned and what functions are available for use when using Eloquent. Maybe I am just missing it.

However, I am using this code in my controller.

public function find($userName){
    $user = UserSecurity::where('userName', $userName)->get();
    //dd($user);
    return Response()->json(['data' => $user], 200);
}

This is my router code.

$router->get('/UserSecurity/find/{userName}', ['uses'=>'UserSecurityController@find']);

I know that it is pulling the correct data from the database as if I uncomment the dd($user) I can see it on screen. However, if I try to send a response through Response()->json(..) it fails with this screen. enter image description here

CodePudding user response:

Using where('...')->get() returns a collection which cannot be used in response()->json(). Try using:

public function find($userName){
    $user = UserSecurity::where('userName', $userName)->first();
    return response()->json(['data' => $user->toArray()], 200);
}
  • Related