SohaIb AhmEd
SohaIb AhmEd

Reputation: 1

Method not allowed exception while updating a record

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpExceptionNomessage

I'm getting this error while trying to update a record in the database. Don't know what's the problem. This question might be a duplicate but I've checked all and couldn't find the answer. Please Help me with this.

Controller Update Method:

public function updateEvent(Request $request, $id=''){

      $name = $request->name;

      $startdate = date_create($request->start_date);

      $start_date = $startdate;

      $time = $request->start_time;

      $start_time = $time;//date("G:i", strtotime($time));

      $endDate = date_create($request->end_date);

      $end_date =$endDate;

      $time_e = $request->end_time;

      $end_time = $time_e;//date("G:i", strtotime($time_e));

      $location = $request->location;

      $description = $request->description;

      $calendar_type = $request->calendar_type;

      $timezone = $request->timezone;

      if ($request->has('isFullDay')) {
        $isFullDay = 1;
      }else{
        $isFullDay = 0;
      }

      DB::table('events')->where('id', $id)->update(
               array(
                   'name' => $name,
                   'start_date' => $start_date,
                   'end_date' => $end_date,
                   'start_time' => $start_time,
                   'end_time' => $end_time,
                   'isFullDay' =>$isFullDay,
                   'description' => $description,
                   'calendar_type' => $calendar_type,
                   'timezone' => $timezone,
                   'location' => $location,
               ));
      // Event Created and saved to the database
      //now we will fetch this events id and store its reminder(if set) to the event_reminder table.

      if(!empty($id))
      {
        if (!empty($request->reminder_type && $request->reminder_number && $request->reminder_duration)) {
          DB::table('event_reminders')->where('id', $id)->update([
            'event_id' => $id,
            'type' => $request->reminder_type,
            'number'=> $request->reminder_number,
            'duration' => $request->reminder_duration
          ]);
        }
      }
      else{
        DB::table('event_reminders')->insert([
          'type' => $request->reminder_type,
          'number'=> $request->reminder_number,
          'duration' => $request->reminder_duration
        ]);
      }

       return redirect()->back();
    }

Route:

Route::post('/event/update/{id}', 'EventTasksController@updateEvent');

Form attributes :

<form action="/event/update/{{$event->id}}" method="POST">
      {{ method_field('PATCH')}}

i'm calling the same update function inside my calendar page and it working fine there. Don't know why it doesn't work here.

Upvotes: 0

Views: 55

Answers (2)

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

Change your route to patch:

Route::patch('/event/update/{id}', 'EventTasksController@updateEvent');

For your comment:

You can send the method to the ajax call by something like data-method attribute on the element you click on,take the method and use it in your ajax call. see this question/answer for help. How to get the data-id attribute?

Upvotes: 1

Mohsin Ali
Mohsin Ali

Reputation: 47

Check the routeing method.

Route::patch('/event/update/{id}', 'EventTasksController@updateEvent');

patch should be the method called on route facade.

Upvotes: 1

Related Questions