更新记录时方法不允许异常

时间:2019-04-06 07:02:32

标签: laravel

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpExceptionNomessage

尝试更新数据库中的记录时遇到此错误。不知道是什么问题。这个问题可能是重复的,但我检查了所有内容却找不到答案。请帮助我。

控制器更新方法:

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::post('/event/update/{id}', 'EventTasksController@updateEvent');

表单属性:

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

我在日历页面中调用了相同的更新功能,并且在该页面上工作正常。不知道为什么它在这里不起作用。

2 个答案:

答案 0 :(得分:1)

检查路由方法。

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

补丁应该是在路线立面上调用的方法。

答案 1 :(得分:1)

将您的路线更改为patch

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

供您评论:

您可以通过单击元素上的data-method属性之类的方法将方法发送到ajax调用,采用该方法并将其用于ajax调用。请参阅此问题/答案以寻求帮助。 How to get the data-id attribute?