MethodNotAllowedHttpException while update record

2019-06-28 08:25发布

问题:

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

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

Here in my routes

Route::resource('product', 'ProductController');

Which gives me the following list of possible routes

| 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   

Editform 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');
   }
}

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

回答1:

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>


回答2:

For update() you need to use PUT method. So, add this line to the form:

<input name="_method" value="PUT" type="hidden">


回答3:

I think the problem is in

<form action="{{url('product/'.$product['id'])}}" class="form-horizontal">

I solved this declaring the form with method="POST" in it, and then: {{ method_field('PUT')}}

    @extends('layouts.app')
@section('title','Product List')

@section('content')

<div class="container">
<form method="POST" 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