Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PetroGromovo's avatar

Why making negative test I got No query results for model error

I send negative id into items.update request:

public function testNegativeUpdatedAsNotFound()
{
    ...
    $itemModel = Item
        ::factory()
        ->languagePrefix($language->prefix)->create([
            'language_id' => $language->id,
        ]);

    $response = $this
        ->put(route('items.update', -$itemModel->id), [ // NEGATIVE VALUE
            'language_id'  => $language->id,
            'title'        => $itemModel->title . ' UPDATED',
        ]);

    $response->assertStatus(JsonResponse::HTTP_NOT_FOUND);  // 404
}

and in control :

public function update(UpdateItemRequest $request, int $itemId)
{
    try {
        $item = $this->itemCrudRepository->update(id: $itemId, data:
            $request->only('language_id', 'title', ...),
                request: $request);    
        return response()->json(['item' => (new ItemResource($item))], JsonResponse::HTTP_RESET_CONTENT); // 205
    } catch (\Exception $e) {    
        return response()->json([
            'error_message' => $e->getMessage(),
        ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // 500
    }    
}

and in repository itemCrudRepository class:

public function update(int $id, array $data, UpdateItemRequest $request): Item
{
    $item = Item::findOrFail($id);

    $item->title = $data['title'];
    ...
    $item->save();
    return $item;
}

Problem is that I got:

No query results for model [App\Models\Item]

error with code 500 , not 404 as I need

How can I fix it ?

Thanks in advance!

0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue is that the findOrFail method in the itemCrudRepository class is throwing an exception when it cannot find the item with the negative ID. To handle this case and return a 404 response instead of a 500 response, you can catch the exception in the update method and throw a ModelNotFoundException with a custom message. Here's an updated version of the update method:

use Illuminate\Database\Eloquent\ModelNotFoundException;

public function update(int $id, array $data, UpdateItemRequest $request): Item
{
    try {
        $item = Item::findOrFail($id);
    } catch (ModelNotFoundException $e) {
        throw new ModelNotFoundException('Item not found', 404);
    }

    $item->title = $data['title'];
    // ...

    $item->save();

    return $item;
}

With this change, when the findOrFail method throws an exception, it will be caught and a ModelNotFoundException with a 404 status code and a custom message will be thrown instead. This will result in a 404 response being returned to the client, as desired.

1 like
m7vm7v's avatar

What is the reason of using negative ID? Usually that is not recommended as primary keys are set to increment and the application would need adjustments. I'd recommend you to switch to positive integers and add a flag column to the database - $table->integer('is_negative'); with maybe better naming.

In your update method it is highly recommended that you validate the request before performing anything.

        $validator = Validator::make([
            'id' => $id
			data
			request
        ], [
            'id' => 'required|exists:items',
        ]);

        if ($validator->fails()) {
            return redirect('...')
                ->withErrors($validator)
                ->withInput();
        }

Hope that helps

Please or to participate in this conversation.