4

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
  • Thanks for asking this question. Commented Mar 20, 2018 at 13:36

3 Answers 3

8

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>
Sign up to request clarification or add additional context in comments.

6 Comments

ok, added. but its gives no response. my URL bar fill with their changed data..!
Show your full form
sorry, which fill form you want?
<form> ... </form>
Check my answer.
|
2

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

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

5 Comments

ok, added. but its gives no response. my URL bar fill with their changed data..!
you are doing the redirection only if created. cover the case when it's not created
Maybe quote a bit from docs how it works
@BugInspector ljubadr is right, you're redirecting only when $created is true
it fixed. Thanks :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.