The PATCH method is not allowed to access role routes. Only supports GET, HEAD and POST methods
P粉986028039
P粉986028039 2023-08-15 19:45:00
0
1
496

I want to use spatie laravel to create roles and permissions. I can't edit my character due to error:

The roles routing of the PATCH method is not supported. Supported methods are: GET, HEAD, POST.

Controller:

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

Blade template:

@csrf @method('PUT')
Return {!! Form::model($role, ['method' => 'PATCH','route' => ['roles.update', $role->id]]) !!}
{{-- --}} {!! Form::text('name', null, array('placeholder' => 'name','class' => 'form-control')) !!}

路由:

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


P粉986028039
P粉986028039

reply all (1)
P粉245276769

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

 @method('PUT') @csrf 
返回
{!! Form::text('permission', null, array('placeholder' => '权限名称', 'class' => 'form-control')) !!}
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!