search

Home  >  Q&A  >  body text

The PATCH method is not allowed to access role routes. Only supports GET, HEAD and POST methods

<p>I want to use spatie laravel to create roles and permissions. I can't edit my character due to error: </p> <blockquote> <p>The roles routing of the PATCH method is not supported. Supported methods are: GET, HEAD, POST. </p> </blockquote> <p><strong>Controller: </strong></p> <pre class="brush:php;toolbar:false;">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 update successful'); }</pre> <p><strong>Blade template:</strong></p> <pre class="brush:php;toolbar:false;"><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">Return</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></pre> <p><strong>路由:</strong></p> <pre class="brush:php;toolbar:false;">Route::group(['middleware' => ['auth']], function() { Route::resource('admin', adminController::class); Route::resource('produk', produkController::class); Route::resource('roles', roleController::class); });</pre> <p><br /></p>
P粉986028039P粉986028039468 days ago606

reply all(1)I'll reply

  • P粉245276769

    P粉2452767692023-08-16 15:10:16

    Add the @method('PUT') directive to the HTML form in the edit view. Normally, the PUT method is used to update resources in a RESTful API, and Laravel automatically treats it as a PATCH request.

    <form action="{{ route('roles.update', $role->id) }}" method="POST">
        @method('PUT')
        @csrf
        <div class="my-3 p-3 bg-body rounded shadow-sm">
            <a href="{{ route('roles.index') }}" class="btn btn-secondary">返回</a>
            <div class="mb-3 row">
                <label for="permission" class="col-sm-2 col-form-label">权限</label>
                <div class="col-sm-10">
                    {!! Form::text('permission', null, array('placeholder' => '权限名称', 'class' => 'form-control')) !!}
                </div>
            </div>
        </div>
        <button type="submit">更新角色</button>
    </form>

    reply
    0
  • Cancelreply