此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。错误

发布于 2025-01-12 21:58:54 字数 8821 浏览 0 评论 0原文

我正在尝试通过网络编辑我的预订,我对 Laravel 还很陌生,我不断收到相同的错误,我认为与我的路线有关。 当我单击提交按钮进行更新时,该路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。显示

我如何修复它?

这是错误异常 MethodNotAllowedHttpException

这是我的代码 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);

这是我的控制器

<?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)
    {
        //
    }
}

这是 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

这是路由列表

 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

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 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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城缀染半城烟沙 2025-01-19 21:58:54

资源为您提供了这些路线(以用户为例):

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

您的问题是在您的刀片文件操作网址中缺少通配符(上例中的 {user} ),因此它调用了错误的网址。

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.

云胡 2025-01-19 21:58:54

尝试在表单标记中使用 route 帮助器。

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

确保您的 $data 有效。如果 $dataModel 类的实例,则可以直接传递它。

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

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])
冰火雁神 2025-01-19 21:58:54

在许多情况下,问题可以通过使用:php artisan route:cache 来解决。

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

白昼 2025-01-19 21:58:54

将表单 action= 属性更改为下一个有关

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

路由绑定

Change your Form action= attribute to the next one

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

More information about route binding

一身骄傲 2025-01-19 21:58:54

与第一个响应相同:检查刀片中的 url

 <form method="POST" action="{{url('admin/reservation/'.$data->id)}}">

是否正确呈现。对没有任何资源的 URL 的 PUT 方法会生成您看到的错误。

Same as for the first response: Check that the url in your blade

 <form method="POST" action="{{url('admin/reservation/'.$data->id)}}">

is correctly rendere. A PUT method towards an URL without any resource generates the error you see.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文