gpt4 book ai didi

php - 在 laravel 中更新记录时出现 MethodNotAllowedHttpException

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:38 25 4
gpt4 key购买 nike

我最近加入了 Laravel 框架,所以首先我尝试在 Laravel 中制作一个 CRUD 系统,以了解它们的基本原理和功能。

现在我在更新现有记录时遇到错误 MethodNotAllowedHttpException

在我的 route

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

编辑窗体 View

    @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 错误。

我尝试了很多解决方案,但都没有解决。所以请指导我哪里出错了。谢谢

最佳答案

这是因为您使用了错误的 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>

关于php - 在 laravel 中更新记录时出现 MethodNotAllowedHttpException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46865073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com