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

yeotifhr  于 12个月前  发布在  PHP
关注(0)|答案(5)|浏览(131)

我试图通过网络编辑我的预订我是很新的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

kxxlusnw

kxxlusnw1#

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

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

字符串
你的问题是,在你的刀片文件中,action url缺少了url(上面例子中的{user}),因此它调用了错误的url。

xmq68pz9

xmq68pz92#

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

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

字符串
确保$data是有效的。如果$dataModel类的示例,则可以直接传递它。

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

yacmzcpb

yacmzcpb3#

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

nukf8bse

nukf8bse4#

将Form action=属性更改为下一个

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

字符串
有关路由绑定的详细信息

2sbarzqh

2sbarzqh5#

与第一个响应相同:检查刀片服务器中的URL

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

字符串
一个没有任何资源的URL的PUT方法会产生你看到的错误。

相关问题