Script Lover
Script Lover

Reputation: 381

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

Upvotes: 2

Views: 2662

Answers (4)

N69S
N69S

Reputation: 17206

There is a big difference between the find() method and the findOrFail() one.

findOrFail() will trigger a ModelNotFound exception when no result is available. To change the output of this kind of exception (without doing a try catch() ) you need to intercept the exception in the render() method in App\Exceptions\Handler::class. tell me if you want an example of this.

another very easy way is to simply do a find.

$UserCars = User::with('cars')->find(request()->user()->id);
//this will return either an instance of User or null
if ($UserCars) {
    $Result = $UserCars->cars;
    $response = new CarResource($Result);
}else{
    $response = response()->json(['data' => 'Resource not found'], 404);
}
return $response;

also since you are recovering the user for the request() helper, there is no need to request the user again from DB, just do it like this.

$user = $request->user();
if ($user) {
    $response = new CarResource($user->cars);
} else {
    $response = response()->json(['data' => 'Resource not found'], 404);
}

return $response;

Upvotes: 2

omitobi
omitobi

Reputation: 7334

First thing: request()->user() already should return Found user. This means that User::with('cars')->findOrFail(request()->user()->id); See still goes to query the db for the user then load related cars.

Secondly, findOrFail() would help you trigger 404 Error as a response so if the user is not found it will never hit this line if ($UserCars !== null) {

Thirdly, I believe CarResource should expect a model and not a Response object hence there might be something going wrong there.

Fourth, Is the route for this controller method declared in the api routes file? I asked then Laravel would handle the response as json for you when there's an error.

My proposal would be this:

$user = $request->user();

if ($user) {
    return new CarResource($user->load('cars'));
}

return response()->json(['data' => 'Resource not found'], 404);

PS: I am not familiar with APIResources but I had an idea of doing so from Fractal Transformer.

Upvotes: 0

Christian Kaal
Christian Kaal

Reputation: 232

You already have the user, so you can just do the following:

// Get the cars from the user    
$cars = request()->user()->cars;
// Verify a filled collection was retrieved    
if ($cars->isNotEmpty()) {
    $result = CarResource::collection($cars);
} else {
    // No cars were found for the current user
    $result = response()->json(['data' => 'Resource not found'], 404);
}

return $result;

Upvotes: 0

user10186369
user10186369

Reputation:

You should try this:

$UserCars = User::with('cars')->findOrFail(request()->user()->id);
if ($UserCars !== null) {
    $Result = $UserCars->cars;

    return new CarResource($Result);

}else{
    return response()->json(['data' => 'Resource not found'], 404);
}

Upvotes: 0

Related Questions