2016-06-08 151 views
1

因此,我可以創建一個新的記錄在我的數據庫與窗體,但不知何故我無法刪除我的數據庫使用窗體而現在,我使用laravel 5。是我的代碼看起來像刪除方法不允許,並返回Laravel 5中的MethodNotAllowedHttpException

routes.php文件

Route::get('/', [ 
    'as' => '/', 
    'uses' => '[email protected]' 
]); 

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

ProductController.php

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 

use DB; 

use App\Product; 

use resources\views\products; 

    class ProductController extends Controller 
    { 
     /** 
     * Display a listing of the resource. 
     * 
     * @return \Illuminate\Http\Response 
     */ 
     public function index() 
     { 
      $product = DB::select('select * from feedback'); 

      return view('products.index') 
       ->with('product',$product); 
     } 

     /** 
     * Show the form for creating a new resource. 
     * 
     * @return \Illuminate\Http\Response 
     */ 
     public function create() 
     { 
      return view('products.create'); 
     } 

     /** 
     * Store a newly created resource in storage. 
     * 
     * @param \Illuminate\Http\Request $request 
     * @return \Illuminate\Http\Response 
     */ 
     public function store(Request $request) 
     { 
      /*$rating = new Product; 
      $rating->Name= $request->name; 
      $rating->avg=$request->price; 
      $rating->save();*/ 

      $inputs= $request->all(); 
      $product= Product::create($inputs); 

      //return redirect()->route('product.index'); 
      return redirect()->action('[email protected]'); 
     } 

     /** 
     * Display the specified resource. 
     * 
     * @param int $id 
     * @return \Illuminate\Http\Response 
     */ 
     public function show($id) 
     { 

      $product= Product::where('idoveralRating',$id)->first(); 

      //return $product; 
      return view('products.show') 
       ->with('product',$product); 
     } 

     /** 
     * Show the form for editing the specified resource. 
     * 
     * @param int $id 
     * @return \Illuminate\Http\Response 
     */ 
     public function edit($id) 
     { 
      // 
     } 

     /** 
     * Update the specified resource in storage. 
     * 
     * @param \Illuminate\Http\Request $request 
     * @param int $id 
     * @return \Illuminate\Http\Response 
     */ 
     public function update(Request $request, $id) 
     { 
      // 
     } 

     /** 
     * Remove the specified resource from storage. 
     * 
     * @param int $id 
     * @return \Illuminate\Http\Response 
     */ 
     public function destroy($id) 
     { 
      //echo '<script>console.log("bitch")</script>'; 
      //Product::destroy($id); 
      $product= Product::where('idoveralRating',$id) 
       ->delete(); 

      return redirect()->route('product.show'); 
     } 
    } 

show.blade.php

@extends('layouts.layout') 
@section('Head') 
    <h1>{{$product}}</h1> 
@stop 
@section('Body') 
<h1>{{$product->Name}}</h1> 
{!!Form::open([ 
    'method' => 'delete', 
    'route'=> array('product.destroy',$product->id), 

])!!} 


{!!Form::submit('Delete')!!} 
{!!Form::close()!!} 
@stop 

index.blade.php

@extends('layouts.layout') 

@section('Head') 
    ALL Product 
@stop 

@section('Body') 

    @foreach($product as $col) 
     <h1>{{$col->Name}}</h1> 
    @endforeach 

@stop 

layout.blade.php

<!DOCTYPE html> 
<html> 
    <head> 
    @yield('Head') 
    </head> 
    <body> 

     @yield('Body') 
    </body> 
</html> 

好了,所以我試着刪除我的數據庫基於我的數據庫ID,我在我的瀏覽器鏈接上鍵入(所以讓我說我類型的產品/ 1),這意味着我想刪除我的我的數據庫ID爲1.

到目前爲止,我已經實現的是,我能夠顯示我的數據庫基於id我鍵入但不知何故,當我想在ProductController類中將id路由到我的銷燬方法,它顯示method =>'delete'不允許,我做錯了什麼?

+1

運行'PHP工匠路線:list',你看在此列表中名稱爲'product.destroy'的路線? –

+0

@AlexeyMezenin運行的命令,它顯示了有product.destroy – Kevin

回答

0

我覺得在你的情況下問題不在刪除路線。在刪除destroy方法中的記錄後,您將返回重定向至product.show路徑,該路徑爲必需參數id
因此,嘗試

public function destroy($id) 
{ 
    $product= Product::where('idoveralRating',$id) 
     ->delete(); 

    return redirect()->route('product.index'); 

} 
+0

的問題是,我無法從我的數據庫中刪除記錄和錯誤仍然相同MethodNotAllowedHttpException – Kevin

1

FORM不要有DELETE方法。

你必須使用這樣的:

在你show.blade.php

{!!Form::open([ 
'method' => 'post', 
'route'=> array('product.destroy',$product->id),])!!} 
{{ method_field('DELETE') }} 
... 
+0

它不工作,不知何故,總是路線product.index甚至儘管我嘗試路由到product.show,我試圖刪除方法場(和方法在我的代碼),但它仍然路線product.index – Kevin

3

嘗試使用 'laravel數據綁定'。 添加到您的RouteServiceProvied.php在引導方法如下代碼:

$router->model('Product', Product::class); 

,並更改在您的控制器破壞方法是:

public function destroy(Product $product) 
{ 
    $product->delete(); 

    return back(); 
} 

而且你在show.blade.php路線文件必須是這樣的:

'route'=> array('product.destroy', $product), 
0

好吧,我想回答我自己的問題,我的代碼中的問題是,我使用的默認主鍵等於id(換句話說,$ primaryKey ='id'),這就是爲什麼當我通過我的$ ID摧毀它似乎因爲我傳遞了不屬於表的主鍵,這就是爲什麼刪除方法似乎無法識別,因爲你不能刪除不存在的東西。

所以問題的解決,是簡單的一步 更改主鍵,保護$的PrimaryKey =「idOveralRating」(我的情況,和它的作品!)

相關問題