laravel 路由角色不支持PATCH方法,支持的方法:GET、HEAD、POST

wa7juj8i  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(178)

我想使用spatie laravel设置角色n权限。由于以下错误,我无法编辑我的角色:
路由角色不支持PATCH方法。支持的方法:GET、HEAD、POST。

控制器:

public function edit(string $id)
{
    $role = Role::find($id);
    $permission = Permission::get();
    $rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id)
        ->pluck('role_has_permissions.permission_id','role_has_permissions.permission_id')
        ->all();

    return view('roles.edit',compact('role','permission','rolePermissions'));
}

public function update(Request $request, string $id)
{
    $this->validate($request, [
        'name' => 'required',
        'permission' => 'required',
    ]);

    $role = Role::find($id);
    $role->name = $request->input('name');
    $role->save();

    $role->syncPermissions($request->input('permission'));

    return redirect()->route('roles.index')
        ->with('success','Role updated successfully');
}

字符串

刀片:

<form action='{{ url('roles/') }}' method='post'>
                        @csrf
                        @method('PUT')
                        <div class="my-3 p-3 bg-body rounded shadow-sm">
                            <a href='{{ url('roles') }}' class="btn btn-secondary">Back</a>

                            {!! Form::model($role, ['method' => 'PATCH','route' => ['roles.update', $role->id]]) !!}
                            
                            <div class="mb-3 row">
                                <label for="permission" class="col-sm-2 col-form-label">Permission</label>
                                <div class="col-sm-10">
                                    {{-- <input type="text" class="form-control" name='permission' value="{{ $role->permission }}" id="permission"> --}}
                                    {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
                                </div>
                            </div>

路线:

Route::group(['middleware' => ['auth']], function() {
    Route::resource('admin', adminController::class);
    Route::resource('produk', produkController::class);
    Route::resource('roles', roleController::class);
});

ql3eal8s

ql3eal8s1#

表单操作没有正确指向更新角色的特定路由。它应该是这样的:

<form action='{{ route('roles.update', $role->id) }}' method='post'>

字符串
@method('PATCH')指令添加到编辑视图中的HTML表单中。

<form action='{{ route('roles.update', $role->id) }}' method='post'>
    @csrf
    @method('PATCH')
    <div class="my-3 p-3 bg-body rounded shadow-sm">
        <a href='{{ url('roles') }}' class="btn btn-secondary">Back</a>

        <div class="mb-3 row">
            <label for="name" class="col-sm-2 col-form-label">Role Name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="name" value="{{ $role->name }}" id="name">
            </div>
        </div>

        <!-- Add your permission selection input here -->

        <button type="submit" class="btn btn-primary">Update Role</button>
    </div>
</form>


最后,确保你清理你的路线。

php artisan route:clear

相关问题