jeudi 1 novembre 2018

How can I make a way to return error message of record not available?

I am trying to fetch user cars, which is working fine. But I want to add if-statement which should return a error message no record found. I am trying as like below

$UserCars = User::with('cars')->findOrFail(request()->user()->id);
if ($UserCars !== null) {
    $Result = $UserCars->cars;
}else{
    $Result = response()->json(['data' => 'Resource not found'], 404);
}

return new CarResource($Result);

The problem is it is working when if-statement true (means when found record in database) but in case of false condition it returns html page with following message

Sorry, the page you are looking for could not be found.

CarResource is API resource which I am using for API's.

Can someone kindly guide me about this I would appreciate. Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire