2016-06-08 145 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”(我的情况,和它的作品!)

相关问题