I am trying to use the Put method and this is my result....
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
http://localhost:3000/admin/genres/
This is the routing:
Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () {
Route::redirect('/', 'records');
Route::resource('genres', 'Admin\GenreController');
Route::get('records', 'Admin\RecordController@index');
});
This is the function in the controller:
public function update(Request $request, Genres $genres)
{
// Validate $request
$this->validate($request,[
'name' => 'required|min:3|unique:genres,name,' . $genres->id
]);
// Update genre
$genres->name = $request->name;
$genres->save();
// Flash a success message to the session
session()->flash('success', 'The genre has been updated');
// Redirect to the master page
return redirect('admin/genres');
}
This is the HTML form code in blade:
@extends('layouts.template')
@section('title', 'Edit genre')
@section('main')
<h1>Edit genre: {{ $genres->name }}</h1>
<form action="/admin/genres/{{ $genres->id }}" method="post">
<!-- this is where is goes wrong i guess... -->
@method('put')
@csrf
<div >
<label for="name">Name</label>
<input type="text" name="name" id="name"
placeholder="Name"
minlength="3"
required
value="{{ old('name', $genres->name ?? '') }}">
@error('name')
<div >{{ $message }}</div>
@enderror
</div>
<button type="submit" >Save genre</button>
</form>
@endsection
This is the routing table:
-------- ---------------------------------------- --------------------------- ------------------ ------------------------------------------------------------------------ ------------
| Domain | Method | URI | Name | Action | Middleware |
-------- ---------------------------------------- --------------------------- ------------------ ------------------------------------------------------------------------ ------------
| | GET|HEAD | / | | Illuminate\Routing\ViewController | web |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | admin | | Illuminate\Routing\RedirectController | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres | genres.index | App\Http\Controllers\Admin\GenreController@index | web |
| | | | | | auth |
| | | | | | admin |
| | POST | admin/genres | genres.store | App\Http\Controllers\Admin\GenreController@store | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres/create | genres.create | App\Http\Controllers\Admin\GenreController@create | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres/{genre} | genres.show | App\Http\Controllers\Admin\GenreController@show | web |
| | | | | | auth |
| | | | | | admin |
| | PUT|PATCH | admin/genres/{genre} | genres.update | App\Http\Controllers\Admin\GenreController@update | web |
| | | | | | auth |
| | | | | | admin |
| | DELETE | admin/genres/{genre} | genres.destroy | App\Http\Controllers\Admin\GenreController@destroy | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/genres/{genre}/edit | genres.edit | App\Http\Controllers\Admin\GenreController@edit | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | admin/records | | App\Http\Controllers\Admin\RecordController@index | web |
| | | | | | auth |
| | | | | | admin |
| | GET|HEAD | api/user | | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | contact-us | | App\Http\Controllers\ContactUsController@show | web |
| | POST | contact-us | | App\Http\Controllers\ContactUsController@sendEmail | web |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web |
| | | | | | auth |
| | GET|HEAD | itunes | | App\Http\Controllers\ItunesController@index | web |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web |
| | | | | | guest |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web |
| | | | | | guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | GET|HEAD | password/confirm | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm | web |
| | | | | | auth |
| | POST | password/confirm | | App\Http\Controllers\Auth\ConfirmPasswordController@confirm | web |
| | | | | | auth |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web |
| | | | | | guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web |
| | | | | | guest |
| | GET|HEAD | shop | | App\Http\Controllers\ShopController@index | web |
| | GET|HEAD | shop/{id} | | App\Http\Controllers\ShopController@show | web |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | user | | Illuminate\Routing\RedirectController | web |
| | | | | | auth |
| | GET|HEAD | user/password | | App\Http\Controllers\User\PasswordController@edit | web |
| | | | | | auth |
| | POST | user/password | | App\Http\Controllers\User\PasswordController@update | web |
| | | | | | auth |
| | GET|HEAD | user/profile | | App\Http\Controllers\User\ProfileController@edit | web |
| | | | | | auth |
| | POST | user/profile | | App\Http\Controllers\User\ProfileController@update | web |
| | | | | | auth |
-------- ---------------------------------------- --------------------------- ------------------ ------------------------------------------------------------------------ ------------
I would really like some help since this is my school project :D my friends project works but we can't figure out why it doesn't work.
Excuse me for my bad english :D
CodePudding user response:
please try updating the action in the form like
<form action="{{ route('genres.update', ['genres' => $genres->id]) }}" method="POST">
@method('PUT')
// and in controller
public function update(Request $request, Genres $genres)
{
dd($genres);
}
CodePudding user response:
try with add $id in update action
like:
public function update($id) { ....
