Laravel Json Response Not working as expected
Asked Answered
D

3

0

Unable to get response while response object is empty. Works perfect when the object has data returned.

public function show($id)
{
    $associates = Associate::find_by_id($id);
    if(count($associates)<1)
    {
        $output = array('message' => 'No Records Found');
        $status = 204;

    }
    else{
        $output = array('message' => 'success','data'=>$associates);
        $status = 200;
    }
    return response()->json($output,$status);
}

There is no response when the $associate object is empty. Response when $associate is not empty:

{
"message": "success",
"data": [
    {
        "first_name": "xxx",
        "last_name": "xxx",
        "mobile": xxxxxxxxxx,
        "email": "xxxxxx@xxxxx",
        "city": "xxxxx",
        "state": "xxxxxx",
        "pincode": "xxxxx"
    }
  ]
}
Deictic answered 6/11, 2018 at 18:48 Comment(2)
try using Associate::find($id) instead of find_by_idBasaltware
@DerekPollard I am using join of multiple tables for this result, can i use Associate::find($id) with joins ?Deictic
P
1

I had the same issue for status code 204 . I believe this is caused here. The Illuminate\Foundation\Application class is then catching this and throwing an HttpException.

I believe the simplest fix would be to make the controller return the following instead:

return Response::make("", 204);

Returning a empty message. check status_code in your code to display message in frontend .

Pvc answered 6/11, 2018 at 19:5 Comment(0)
P
0

It will be easier if you use route model binding to find the ID of the record. For more information check https://laravel.com/docs/5.7/routing#route-model-binding.

I think the snippet below should work.

if ($associates) {
    $output = array('message' => 'success','data'=>$associates);
    $status = 200;
} else {
    $output = array('message' => 'No Records Found');
    $status = 204;
}
Patch answered 6/11, 2018 at 19:19 Comment(0)
C
0

I've rewrote the function for your reference.

BTW. If the function return only one record, use singular noun for variable name in general.

public function show($id)
{
    // Use find() instead of find_by_id()
    $associate = Associate::find($id);

    // $associate will be null if not matching any record.
    if (is_null($associate)) {

        // If $associate is null, return error message right away.
        return response()->json([
            'message' => 'No Records Found',
        ], 204);
    }

    // Or return matches data at the end.
    return response()->json([
        'message' => 'success',
        'data' => $associate,
    ], 204);
}
Chiseler answered 7/11, 2018 at 4:9 Comment(2)
not working, tried it. And yes i am using multiple joins, so i use my custom model function.Deictic
Okay, I get it now why you're using custom function. But what value will return when not matching any records in your function? My point is that value could be the key.Chiseler

© 2022 - 2024 — McMap. All rights reserved.