laravel中的更新记录时发生MethodNotAllowedHttpException [英] MethodNotAllowedHttpException while update record in laravel

查看:78
本文介绍了laravel中的更新记录时发生MethodNotAllowedHttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近加入了Laravel框架,因此我首先尝试在laravel中制作一个CRUD系统,以了解其基本功能.

I've recently joined Laravel framework so firstly i trying to make a CRUD system in laravel, for understanding their fundamentals their functionality.

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

Now here i've face a error MethodNotAllowedHttpException at the time of updating existing record.

在我的路线上

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 错误.

In this situation, whenever i wan to change existing records data by refiling edit form. it generates MethodNotAllowedHttpException error.

我尝试了很多解决方案,但没有解决.因此,请指导我要去哪里错了.谢谢

i tried lots of solutions but it not fixed. So please guide where i am going wrong. Thanks

推荐答案

这是因为您使用错误的HTTP方法来更新现有记录.您需要通过方法欺骗将方法指定为PUT. 试试这个:

It's because you are using the wrong HTTP method for updating existing record. You need to specify method as PUT via method spoofing. Try this:

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

这篇关于laravel中的更新记录时发生MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆