此路由不支持DELETE方法。支持的方法:GET,HEAD,POST不起作用

时间:2019-11-30 06:30:24

标签: php laravel

我要删除预订,这是我的表格

 <form action="{{ route('bookings.delete', $booking->id) }}" method="POST">
                        @csrf
                        @method('delete')
                        <button type="submit" class="btn btn-outline-danger">Delete</button>
   </form>

这是我的控制器

public function delete($id){
    $booking = Booking::find($id);
    $booking->delete();
}

这是我的路线

Route::post('/bookings/delete', 'BookingController@delete')->name('bookings/delete');

3 个答案:

答案 0 :(得分:2)

您的路线应定义为

Route::delete('/bookings/delete/{id}', 'BookingController@delete')->name('bookings.delete');

您还应该将id作为路由参数传递

在控制器的删除方法中

public function delete($id) 
{
         $booking = Booking::findOrFail($id);
         $booking->delete();

         return redirect()->route('bookings.index');
}

答案 1 :(得分:1)

在您的路线文件中

name('bookings/delete'); 

应该是

name('bookings.delete');

尝试一下,让我们知道

答案 2 :(得分:0)

如果您需要使用@method('delete'),则您的路线应为delete()而不是post

Route::delete('/bookings/delete/{id}', 'BookingController@delete')->name('bookings.delete');

和形式:

<form action="{{ route('bookings.delete', $booking->id) }}" method="POST">
    @csrf
    @method('delete')
    <button type="submit" class="btn btn-outline-danger">Delete</button>
</form>