'The PUT method is not supported for this route. Supported methods: GET, HEAD, POST. Error
Hello i am trying to edit my reservation through the web I am pretty new to laravel I keep getting the same error I think it has to do with my routes. when i click submit button for update The PUT method is not supported for this route. Supported methods: GET, HEAD, POST. show up
how i can fix it?
this is the error exception MethodNotAllowedHttpException
here is my code web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Reservationcontroller;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('home');
});
// Admin Dashoard
Route::get('admin', function () {
return view('dashboard');
});
// Reservation Controller
Route::get('admin/reservation/{id}/delete',[Reservationcontroller::class, 'destroy']);
Route::resource('admin/reservation',Reservationcontroller::class);
this is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\models\Reservation;
class Reservationcontroller extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data=Reservation::all(); //show data in reservation page
return view('reservation.index',['data'=>$data]);
//pass data in reservation page
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('reservation.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data= new Reservation;
$data->date=$request->date;
$data->name=$request->name;
$data->description=$request->description;
$data->invoicenumber=$request->invoicenumber;
$data->passengerid=$request->passengerid;
$data->save();
return redirect('admin/reservation/create')->with('success','Reservation has been added.');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$data=Reservation::find($id); //find reservation
return view('reservation.show',['data'=>$data]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$data=Reservation::find($id); //find reservation
return view('reservation.edit',['data'=>$data]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$data=Reservation::find($id);
$data->date=$request->date;
$data->name=$request->name;
$data->description=$request->description;
$data->invoicenumber=$request->invoicenumber;
$data->passengerid=$request->passengerid;
$data->save();
return redirect('admin/reservation/'.$id.'/edit')->with('success','Reservation data has been updated.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
This is edit.blade.php
@extends('layout')
@section('content')
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Edit Reservation
<a href="{{url('admin/reservation')}}" class="float-right btn btn-success btn-sm">View All Reservation</a>
</h6>
</div>
<div class="card-body">
@if(Session::has('success'))
<p class="text-success">{{session('success')}}</p>
@endif
<div class="table-responsive">
<form method="POST" action="{{url('admin/reservation/'.$data->id)}}">
@csrf
@method('put')
<table class="table table-bordered" >
<tr>
<th>Date</th>
<td><input value="{{$data->date}}" name="date" type="datetime-local" class="form-controll" /></td>
</tr>
<tr>
<th>Name</th>
<td><input value="{{$data->name}}" name="name" type="text" class="form-control" /></td>
</tr>
<tr>
<th>Description</th>
<td><textarea name="description" class="form-control">{{$data->description}}</textarea></td>
<tr>
<th>InvoiceNumber</th>
<td><input value="{{$data->invoicenumber}}" name="invoicenumber" type="number" class="form-control"></td>
</tr>
<tr>
<th>PassengerID</th>
<td><input value="{{$data->passengerid}}" name="passengerid" type="number" class="form-control"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" class="btn btn-primary">
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
@endsection
This is route list
GET|HEAD / ..................................................................................................
POST _ignition/execute-solution ignition.executeSolution › Spatie\LaravelIgnition › ExecuteSolutionContr…
GET|HEAD _ignition/health-check ....... ignition.healthCheck › Spatie\LaravelIgnition › HealthCheckController
POST _ignition/update-config .... ignition.updateConfig › Spatie\LaravelIgnition › UpdateConfigController
GET|HEAD admin ..............................................................................................
GET|HEAD admin/reservation .................................. reservation.index › Reservationcontroller@index
POST admin/reservation .................................. reservation.store › Reservationcontroller@store
GET|HEAD admin/reservation/create ......................... reservation.create › Reservationcontroller@create
GET|HEAD admin/reservation/{id}/delete ........................................ Reservationcontroller@destroy
GET|HEAD admin/reservation/{reservation} ...................... reservation.show › Reservationcontroller@show
PUT|PATCH admin/reservation/{reservation} .................. reservation.update › Reservationcontroller@update
DELETE admin/reservation/{reservation} ................ reservation.destroy › Reservationcontroller@destroy
GET|HEAD admin/reservation/{reservation}/edit ................. reservation.edit › Reservationcontroller@edit
GET|HEAD api/user ...........................................................................................
GET|HEAD sanctum/csrf-cookie .................................... Laravel\Sanctum › CsrfCookieController@show
Solution 1:[1]
resource creates your routes will be like this.
route::get('admin/reservation',Reservationcontroller@index);
route::get('admin/reservation/create',Reservationcontroller@create);
route::post('admin/reservation/store',Reservationcontroller@store)
route::get('admin/reservation/{id}',Reservationcontroller@show);
route::get('admin/reservation/{id}/edit',Reservationcontroller@edit);
route::put('admin/reservation/{id}',Reservationcontroller@update)
route::delete('admin/reservation/{id}',Reservationcontroller@destroy)
In the blade file use the route by below.
<form method="POST" action="{{url('admin/reservation/'.[$data->id])}}">
@csrf
@method("PUT")
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dip Ghosh |
