此路由不支持PUT方法。支持的方法:GET,HEAD。 (使用资源)

时间:2019-11-02 17:29:31

标签: php laravel

我已经见过几次这个问题,但是形式不正确。

这是我的: edit.blade.php

<form action="{{ route('admin.korisnici.update', ['korisnik' => $korisnik->id]) }}" method="POST">
    {{csrf_field()}}
    {{method_field('PUT')}}
    @foreach($uloge as $uloga)
        <div class="form-check">
            <input type="checkbox" name="uloge[]" value="{{ $uloga->id }}" 
            {{ $korisnik->hasRole($uloga->naziv_tipa)?'checked':'' }}>
            <label>{{ $uloga->naziv_tipa }}</label>
        </div>
    @endforeach
    <button type="submit" class="btn btn-primary btn-sm">Ažuriraj</button>
</form>

我的控制器: KorisniciController.php

public function update(Request $request, $id)
{
        if (Auth::user()->id == $id) {
            return redirect()->route('admin.korisnici.index');
        }

        $korisnik = User::find($id);
        $korisnik->roles()->sync($request->uloge);

    return redirect()->route('admin.korisnici.index');
}

还有我的 web.app

Route::namespace('Admin')->prefix('admin')->middleware(['auth', 'roles.admin'])->name('admin.')->group(function(){
    Route::resource('/korisnici', 'KorisniciController')->except(['show', 'create', 'store']);
});

php artisan route:list返回

+--------+-----------+----------------------------------+-------------------------+------------------------------------------------------------------------+--------------------------+
| Domain | Method    | URI                              | Name                    | Action                                                                 | Middleware               |
+--------+-----------+----------------------------------+-------------------------+------------------------------------------------------------------------+--------------------------+
|        | GET|HEAD  | /                                |                         | Closure                                                                | web                      |
|        | GET|HEAD  | admin                            |                         | Closure                                                                | web,auth,roles.admin     |
|        | GET|HEAD  | admin/korisnici                  | admin.korisnici.index   | App\Http\Controllers\Admin\KorisniciController@index                   | web,auth,roles.admin     |
|        | DELETE    | admin/korisnici/{korisnici}      | admin.korisnici.destroy | App\Http\Controllers\Admin\KorisniciController@destroy                 | web,auth,roles.admin     |
|        | PUT|PATCH | admin/korisnici/{korisnici}      | admin.korisnici.update  | App\Http\Controllers\Admin\KorisniciController@update                  | web,auth,roles.admin     |
|        | GET|HEAD  | admin/korisnici/{korisnici}/edit | admin.korisnici.edit    | App\Http\Controllers\Admin\KorisniciController@edit                    | web,auth,roles.admin     |

2 个答案:

答案 0 :(得分:2)

路线期望参数korisnici,但您通过了korisnik

实际上,您根本不需要指定密钥,只需将模型传递给路线

<form action="{{ route('admin.korisnici.update', $korisnik) }}" 

答案 1 :(得分:0)

是否可能是您的情况,但这一次发生在我身上,解决方案是清除路由缓存:

php artisan route:cache