laravel 此路由不支持该方法,支持的方法:GET、HEAD、POST

xoshrz7s  于 2023-10-22  发布在  其他
关注(0)|答案(6)|浏览(127)

我正在创建一个显示一些数据的索引表单。一切都准备好了,但当我删除按钮,我得到一个错误“该方法不支持此路由。支持的方法: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
yizd12fk

yizd12fk1#

确保您的表单没有包含在另一个表单中。我犯了这个愚蠢的错误,得到了同样的错误消息。

z2acfund

z2acfund2#

这是因为你传递了POST方法作为你的表单的方法,这是错误的,正确的做法是传递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');
}
mdfafbf1

mdfafbf13#

看起来你快到了!我会使用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();

剩下的代码应该没问题。如果不行就告诉我。

qv7cva1a

qv7cva1a4#

在Form中使用{{ csrf_field() }}{{ method_field('DELETE') }}

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

在Controller中使用此

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

如果我们使用的是Route::resource(),那么它将通过destroy函数自动路由。

hmmo2u0o

hmmo2u0o5#

忘记在开始时使用slash:

<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';
}
cuxqih21

cuxqih216#

查看

<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');

相关问题