此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。错误
我正在尝试通过网络编辑我的预订,我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
资源为您提供了这些路线(以用户为例):
您的问题是在您的刀片文件操作网址中缺少通配符(上例中的 {user} ),因此它调用了错误的网址。
Resource gives you these routes (with user as example):
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.
尝试在表单标记中使用
route
帮助器。确保您的
$data
有效。如果$data
是Model
类的实例,则可以直接传递它。Try using the
route
helper in your form tag.Make sure your
$data
is valid. If$data
is an instance of aModel
class, you can pass it directly.在许多情况下,问题可以通过使用:
php artisan route:cache
来解决。In many cases the problem solved by using:
php artisan route:cache
.将表单
action=
属性更改为下一个有关路由绑定
Change your Form
action=
attribute to the next oneMore information about route binding
与第一个响应相同:检查刀片中的 url
是否正确呈现。对没有任何资源的 URL 的 PUT 方法会生成您看到的错误。
Same as for the first response: Check that the url in your blade
is correctly rendere. A PUT method towards an URL without any resource generates the error you see.