melon-lord's avatar

Update method results in 404 not found when using ->update()

Hi, im still new to laravel, and just starting out the basics, ive just made a controller that handles a Model's update using the ->update() method. It works well, but ive realized if im performing an update with no changes on any fields whatsoever, it returns a 404 not found instead in my Postman Client.

Any idea what might cause this ? I have only searched for the docs and didnt' found anything related to 404 responses. And if you might wonder:

"Did you pass the correct id ?" => "Yes I passed the correct id" "Route Model Binding?" => "Yes I use route model binding"

Simply put, i passed the exact same request that works the first time, but not the second time (no changes on the fields/attributes)

0 likes
2 replies
martinbean's avatar

@melon-lord We have no idea without seeing any code, or what requests you’re making.

melon-lord's avatar

@martinbean Its a simple update method like this,

public function update(Request $request, School $school)
    {
        return $this->execute(function () use ($request, $school) {
            $school->update($request->all());
            return $this->successResponse('Resource updated successfully.');
        });
    }

where this controller extends a base controller that has the execute function of:

protected function execute(callable $callback)
    {
        try {
            return $callback();
        } 
        catch (ModelNotFoundException $e) {
            return $this->errorResponse('Resource not found', 404);
        } 
        catch (ValidationException $e) {
            return $this->errorResponse('Validation error', 422, $e->errors());
        } 
        catch (QueryException $e) {
            return $this->errorResponse('Database error', 500);
        } 
        catch (Exception $e) {
            return $this->errorResponse('An unexpected error occurred', 500);
        }
    }

now suppose, ive made a PUT/PATCH request with the following request body:

{
    "name": "My Senior High"
}

when my current record in database has the name, say 'My Other Senior High', it works. but if i make another request again with the same request body (now the database record's name is 'My Senior High') it results in a 404 response.

Please or to participate in this conversation.