skip to Main Content

I used post method in laravel still getting error 404.

Here is my code:

TaskController.php

 public function changeStatus(Request $request, $id)
    {
        try {
            DB::beginTransaction();
            $task = Task::find($id);
            if (!$task) {
                return Helper::error(__('messages.task_not_found'), 404);
            }
            $input = $request->all();
            $validator = Validator::make($input, [
                'status' => 'required|boolean'
            ]);
            if ($validator->fails()) {
                return Helper::error($validator->errors()->first(), 422);
            }
            $task->status = $input['status'];
            $task->save();
            DB::commit();
            return Helper::success(__('messages.task_status_updated'), new someDetailResource($task), 200);
        } catch (Throwable $th) {
            report($th);
            DB::rollBack();
            return Helper::error(__('messages.something_went_wrong'), 500);
        }
    }

api.php

Route::prefix('task')->controller(TaskController::class)->group(function () {
        Route::post('changeStatus/{id}', 'changeStatus');
});

In Postman

http://127.0.0.1:8000/api/task/changeStatus/6

I am getting 404 error in Postman.

Can anyone help me with this?
Thanks for considering my request.

I have double verified. All things are correct.

2

Answers


  1. Chosen as BEST ANSWER

    First check if your route is registered or not, to check you can use the following command in the terminal. This command will display all the registered routes in your Laravel application. php artisan r:l

    If your route is not registered, it might be due to cached data. To clear the cache, run the following command: php artisan optimize:clear .

    This will clear any cached data and ensure that your routes are up-to-date.

    Check this link about clear cache:

    In my case this will work perfectly.


  2. The problem might not be in your routing, you are not finding any task so the service return a 404, as pointed by @geertjanknapen:

    public function changeStatus(Request $request, $id)
        {
            try {
                DB::beginTransaction();
                $task = Task::find($id);
                if (!$task) {
                    //This is what you need to debug
                    return Helper::error(__('messages.task_not_found'), 404);
                }
    ...
            
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search