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

时间:2019-05-24 13:06:45

标签: laravel

我正在创建一个显示一些数据的索引表单。一切就绪,但是当我按下删除按钮时,出现错误“此路由不支持DELETE方法。受支持的方法:GET,HEAD,POST。”

路线

Route::group(['middleware' => ['auth']], function() {
    Route::resource('roles','RoleController');
    Route::resource('users','UserController');
    Route::resource('kamar_theresia','Kamar_TheresiaController');
});

控制器

public function destroy($id)
    {
        Kamar_Theresia::find($id)->delete();
        return redirect()->route('kamar_theresia.index')
                        ->with('success','Kamar Theresia deleted successfully');
    }

查看

        @foreach ($kamar_theresia as $tere)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $tere->nama }}</td>
            <td>{{ $tere->name }}</td>
            <td>{{ $tere->ketersediaan }}</td>
            <td>
                @can('theresia-delete')
                {!! Form::open(['method' => 'DELETE','route' => ['kamar_theresia.destroy', $tere->id],'style'=>'display:inline']) !!}
                {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
                {!! Form::close() !!}
                @endcan
            </td>
        </tr>
        @endforeach

6 个答案:

答案 0 :(得分:1)

那是因为您正在传递DELETE方法作为表单方法,这是错误的,正确的做法是传递de POST方法。

检查此示例:

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

您的控制者应为:

public function destroy(Kamar_Theresia $khamar_teresia)
{
    $khamar_teresia->delete();
    return redirect()->route('kamar_theresia.index')
                    ->with('success','Kamar Theresia deleted successfully');
}

答案 1 :(得分:0)

使用{{ csrf_field() }}{{ method_field('DELETE') }}进入表格。

 {{ csrf_field() }}
 {{ method_field('DELETE') }} 

将其用于控制​​器

public function destroy($id)
    {
       $delete = kamar_theresia::find($id);
       $delete->delete();
       return redirect('/')->with('deleted','Kamar Theresia deleted successfully');
    }

如果我们使用的是Route::resource(),它将自动使用destroy功能进行路由。

答案 2 :(得分:0)

好像您快到了!我将使用POST来实现类似于以下形式:

{{ Form::open(['method' => 'POST', 'route' => ['kamar_theresia.destroy']) }}
    {{ Form::hidden('id',$tere->id) }}
    {{ Form::submit('Delete') }}
{{ Form::close() }}

,然后在您的控制器中

public function destroy(Request $request){
    $id = $request->input('id');
    Kamar_Theresia::find($id)->delete();

其余的代码应该没问题。让我知道这是否行不通。

答案 3 :(得分:0)

忘记在开始时将斜杠付诸实践:

<form method="POST" action={{--here=> --}}"/save_edit_delete_order/{{$order_id}}">  
            @csrf
            @method('delete')
......

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Yes, I am</button>
                </div>

            </div>
        </form>

在资源控制器中:

public function destroy($id)
{
    return 'kuku';
}

答案 4 :(得分:0)

视图

<form action="{{route('command.delete',[$command->id,$command->car_id])}}" method="post">
@csrf
{{method_field('delete')}}
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>

网络

Route::delete('/commands/{commandId}/{carId}/delete','CommandController@deleteUserCommands')->name('command.delete');

答案 5 :(得分:0)

确保您的表格没有其他表格。我犯了这个愚蠢的错误,并得到了相同的错误消息。