2017-10-21 85 views
2

我最近加入了Laravel框架,所以首先我試圖在laravel中創建一個CRUD系統,以瞭解他們的基本功能。MethodNotAllowedHttpException while laravel更新記錄

現在在這裏,我在更新現有記錄的時間面對錯誤MethodNotAllowedHttpException

在這裏,在我的路線

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

這給了我可能的路線

| GET|HEAD | product     | product.index   | App\Http\Controllers\[email protected] 
| POST  | product     | product.store   | App\Http\Controllers\[email protected]  
| GET|HEAD | product/create    | product.create   | App\Http\Controllers\[email protected]  
| GET|HEAD | product/{product}   | product.show   | App\Http\Controllers\[email protected]   
| DELETE | product/{product}   | product.destroy  | App\Http\Controllers\[email protected]   
| PUT|PATCH | product/{product}   | product.update   | App\Http\Controllers\[email protected]  
| GET|HEAD | product/{product}/edit  | product.edit   | App\Http\Controllers\[email protected] 

Editform視圖

@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 
以下列表10

ProductController.php

public function update(Request $request, $product_id) 
{ 
    $created = product::create($request->all()); 
    if($created){ 
    return redirect('product')->with('message','data added'); 
    } 
} 

在這種情況下,每當我婉refiling編輯的形式來改變現有的記錄數據。它會生成MethodNotAllowedHttpException錯誤。

我嘗試了很多解決方案,但沒有修復。所以請指導我出錯的地方。謝謝

回答

2

這是因爲您正在使用錯誤的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> 
+0

好的,補充。但它沒有迴應。我的網址欄填入他們已更改的數據..! –

+0

顯示完整的表格 –

+0

對不起,您需要填寫哪種表格? –

1

對於update()您需要使用PUT方法。因此,將此行添加到表單中:

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

好的,補充。但它沒有迴應。我的網址欄填入他們已更改的數據..! –

+1

只在創建時才執行重定向。覆蓋未創建的案例 – ljubadr

+0

也許引用[位文檔](https://laravel.com/docs/5.5/routing#form-method-spoofing)它是如何工作的 – ljubadr