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
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>
For update()
you need to use PUT
method. So, add this line to the form:
<input name="_method" value="PUT" type="hidden">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With