我最近加入了Laravel框架,所以我首先尝试在laravel中创建一个CRUD系统,以了解它们的基本功能。
现在,我在更新现有记录时遇到错误 MethodNotAllowedHttpException 。
此处在我的路线中
Route::resource('product', 'ProductController');
这给了我以下可能路线列表
| GET|HEAD | product | product.index | App\Http\Controllers\ProductController@index
| POST | product | product.store | App\Http\Controllers\ProductController@store
| GET|HEAD | product/create | product.create | App\Http\Controllers\ProductController@create
| GET|HEAD | product/{product} | product.show | App\Http\Controllers\ProductController@show
| DELETE | product/{product} | product.destroy | App\Http\Controllers\ProductController@destroy
| PUT|PATCH | product/{product} | product.update | App\Http\Controllers\ProductController@update
| GET|HEAD | product/{product}/edit | product.edit | App\Http\Controllers\ProductController@edit
编辑表格视图
@extends('layouts.app')
@section('title','Product List')
@section('content')
<div class="container">
<form action="{{url('product/'.$product['id'])}}" class="form-horizontal">
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
{{csrf_field()}}
{{ method_field('PUT')}}
<!-- <input name="_method" type="hidden" value="PUT"> -->
<input type="text" name="name" class="form-control" id="exampleInputEmail1" value="{{$product['name']}}" aria-describedby="emailHelp" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">category</label>
<input type="text" value="{{$product['category']}}" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="category" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">weight</label>
<input type="text" value="{{$product['weight']}}" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="weight" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">price</label>
<input type="text" value="{{$product['price']}}" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="price" placeholder="">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
@endsection
ProductController.php
public function update(Request $request, $product_id)
{
$created = product::create($request->all());
if($created){
return redirect('product')->with('message','data added');
}
}
在这种情况下,每当我想通过重新编辑表单来更改现有记录数据时。它会生成 MethodNotAllowedHttpException 错误。
我尝试了很多解决方案,但没有修复。所以请指导我哪里出错了。感谢
答案 0 :(得分:7)
这是因为您使用了错误的HTTP方法来更新现有记录。您需要通过方法欺骗将方法指定为PUT
。
试试这个:
<div class="container">
<form action="{{ route('product.update', $product['id']) }}" method="POST" class="form-horizontal">
{{ csrf_field() }}
{{ method_field('PUT')}} //method Spoofing
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" name="name" class="form-control" id="exampleInputEmail1" value="{{ $product['name'] }}" aria-describedby="emailHelp" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">category</label>
<input type="text" value="{{$product['category']}}" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="category" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">weight</label>
<input type="text" value="{{$product['weight']}}" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="weight" placeholder="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">price</label>
<input type="text" value="{{$product['price']}}" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="price" placeholder="">
</div>
<input type="submit" name="submit" class="btn btn-primary">Save</button> //input type, not button
</form>
</div>
答案 1 :(得分:2)
对于update()
,您需要使用PUT
方法。因此,请将此行添加到表单中:
<input name="_method" value="PUT" type="hidden">
答案 2 :(得分:0)
我认为问题出在
Factory
我解决了这个问题,在其中声明了方法=“ POST”,然后:{{method_field('PUT')}}
<form action="{{url('product/'.$product['id'])}}" class="form-horizontal">