0

I am trying to update my database via a PUT method:

      const eventid = arg.event.id;
      const eventData = {
        start: arg.event.start.toISOString(),
        end: arg.event.end.toISOString(),
      };
      const csrfToken = document.head.querySelector("[name~=csrf-token][content]").content;
      console.log(csrfToken);
      fetch(`/api/event/update/${eventid}`, {
        method: 'PUT',
        headers: {
          "X-CSRF-Token": csrfToken
        },
        body: encodeFormData(eventData),
      })

The specific error is `PUT HTTP://localhost:8000/api/event/update/105 405 (Method not allowed). I have tried other methods but they all return similar errors. Additionally here is my Controller code:

    public function update(Request $request)
    {
        $booking = Booking::findOrFail($request->id);
        $booking->start_date = $request->start;
        $booking->end_date = $request->end;
        $booking->save();

        return response()->json($booking);
    }

Route:

Route::post('/event/update/{eventid}', [CalendarController::class, 'update']);

Is this an issue with using 'post' in my route?

15
  • Hi, If you are using PUT method, you need to use Route::put('/event/update/{eventid}', [CalendarController::class, 'update']);, You could also make your method POST Commented Mar 3, 2022 at 15:13
  • Thanks, i've corrected it now but it is providing a 404 error in place instead. Commented Mar 3, 2022 at 15:17
  • I see you're calling to /api/event/update/${eventid} and the route is /event/update/{eventid} Is the route nested in a group for api prefix(or api.php file)? Commented Mar 3, 2022 at 15:20
  • Yes, it is located within my api.php file which is why I'm confused as to why it can't locate it properly. Commented Mar 3, 2022 at 15:22
  • can you type php artisan route:list in cli to see your actual registered routes? Commented Mar 3, 2022 at 15:23

1 Answer 1

0

The best way to know that your variables return what you expect them to return is to test as you code.

From my experience, I find that when I dd() variables at every point I declare and instantiate a variable, especially those passed to a function, it saves me a lot of headache.

Change your update method needs to change from:

  public function update(Request $request)
    {
        $booking = Booking::findOrFail($request->id);
        $booking->start_date = $request->start;
        $booking->end_date = $request->end;
        $booking->save();

        return response()->json($booking);
    }

To:

   public function update(Request $request, $event_id)
    {
        $booking = Booking::findOrFail($event_id);
        $booking->start_date = $request->start;
        $booking->end_date = $request->end;
        $booking->save();

        return response()->json($booking);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.