不允許用PATCH方法存取角色路由。只支援GET、HEAD和POST方法
<p>我想使用spatie laravel來建立角色和權限。由於錯誤,我無法編輯我的角色:</p>
<blockquote>
<p>不支援PATCH方法的roles路由。支援的方法有:GET、HEAD、POST。 </p>
</blockquote>
<p><strong>控制器:</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','角色更新成功');
}</pre>
<p><strong>Blade範本:</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">返回</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">權限</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' => '名稱','class' => 'form-control')) !!}
</div>
</div></前>
<p><strong>路由:</strong></p>
Route::group(['middleware' => ['auth']], function() {
路線::資源('admin', adminController::class);
路線::資源('produk', produkController::class);
路線::資源('角色', roleController::class);
});</pre>
<p><br />></p>