Home > OS >  The PUT method is not supported for this route. Supported methods: GET, HEAD, POST. Error
The PUT method is not supported for this route. Supported methods: GET, HEAD, POST. Error

Time:03-08

I am trying to edit my reservation through the web I am pretty new to Laravel I keep getting the same error I think has to do with my route. 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 >

    <!-- DataTales Example -->
    <div >
        <div >
            <h6 >Edit Reservation
                <a href="{{url('admin/reservation')}}" >View All Reservation</a> 
            </h6>
        </div>
        <div >
            @if(Session::has('success'))
                <p >{{session('success')}}</p>
            @endif
            <div >
                <form method="POST" action="{{url('admin/reservation/'.$data->id)}}">
                    @csrf                
                    @method('put')
                    <table >
                        <tr>
                            <th>Date</th>
                            <td>
                                <input value="{{$data->date}}" name="date" type="datetime-local"  />
                            </td>
                        </tr>
                        <tr>
                            <th>Name</th>
                            <td>
                                <input value="{{$data->name}}" name="name" type="text"  />
                            </td>
                        </tr>    
                        <tr>
                            <th>Description</th>
                            <td>
                                <textarea name="description" >{{$data->description}}</textarea>
                            </td>
                        <tr>
                            <th>InvoiceNumber</th>
                            <td>
                                <input value="{{$data->invoicenumber}}" name="invoicenumber" type="number" >
                            </td>
                        </tr>
                        <tr>
                            <th>PassengerID</th>
                            <td>
                                <input value="{{$data->passengerid}}" name="passengerid" type="number" >
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <input type="submit" >
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- /.container-fluid -->
@endsection

This is the 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

CodePudding user response:

Try using the route helper in your form tag.

<form method="POST" action="{{ route('reservations.update', [$data->id]) }}">
    @csrf
    @method('put')
    ...
</form>

Make sure your $data is valid. If $data is an instance of a Model class, you can pass it directly.

route('reservations.update', [$data])

CodePudding user response:

In many cases the problem solved by using: php artisan route:cache.

CodePudding user response:

Resource gives you these routes (with user as example):

Verb          Path                        Action  Route Name
GET           /users                      index   users.index
GET           /users/create               create  users.create
POST          /users                      store   users.store
GET           /users/{user}               show    users.show
GET           /users/{user}/edit          edit    users.edit
PUT|PATCH     /users/{user}               update  users.update
DELETE        /users/{user}               destroy users.destroy

Your problem is that in your blade file action url is missing the wildcard ({user} in above example), therefore it's calling the wrong url.

CodePudding user response:

Change your Form action= attribute to the next one

{{ route('reservation.update', ['reservation' => $data->id]) }}

More information about route binding

  • Related