不允许删除方法并在 Laravel 5 中返回 MethodNotAllowedHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37699138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
delete method not allowed and returning MethodNotAllowedHttpException in Laravel 5
提问by Kevin
So I can create a new record on my database with form, but somehow i fail to delete my database using form And right now, I'm using laravel 5. So this is my code looks like
所以我可以用表单在我的数据库上创建一个新记录,但不知何故我无法使用表单删除我的数据库现在,我使用的是 laravel 5。所以这是我的代码
routes.php
路由文件
Route::get('/', [
'as' => '/',
'uses' => 'PagesController@getIndex'
]);
Route::resource('product', 'ProductController');
ProductController.php
产品控制器.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('ProductController@index');
}
/**
* 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
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
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
layout.blade.php
<!DOCTYPE html>
<html>
<head>
@yield('Head')
</head>
<body>
@yield('Body')
</body>
</html>
Ok, So im trying to delete my database based on my database id that i typed onmy browser link(so let say i type product/1),it means i want to delete my my database with id of 1.
好的,所以我试图根据我在浏览器链接上输入的数据库 ID 删除我的数据库(假设我输入 product/1),这意味着我想删除 ID 为 1 的数据库。
What I've achieve so far is that I'm able to show my database based on id i typed but somehow when i want to route the id to my destroy method in ProductController class, it shows that method=>'delete' not allowed,what am i do wrong?
到目前为止,我所取得的成就是,我能够根据我输入的 id 显示我的数据库,但是当我想将 id 路由到 ProductController 类中的 destroy 方法时,它显示不允许 method=>'delete' ,我做错了什么?
回答by Paul Androschuk
Try to use 'laravel data binding'. Add to your RouteServiceProvied.php in boot method following code:
尝试使用“laravel 数据绑定”。在引导方法中添加到您的 RouteServiceProvied.php 以下代码:
$router->model('Product', Product::class);
And change destroymethod at your controller to this:
并将控制器上的destroy方法更改为:
public function destroy(Product $product)
{
$product->delete();
return back();
}
And your route at show.blade.phpfile must be like this:
你在show.blade.php文件中的路由必须是这样的:
'route'=> array('product.destroy', $product),
回答by Autista_z
FORM dont have DELETE method.
FORM 没有 DELETE 方法。
You have to use it like this:
你必须像这样使用它:
In your show.blade.php
在你的 show.blade.php
{!!Form::open([
'method' => 'post',
'route'=> array('product.destroy',$product->id),])!!}
{{ method_field('DELETE') }}
...
回答by huuuk
I think problem in your case not in delete route. After you delete record in destroy
method, you return redirect to product.show
route which is required parameter id
.
So try
我认为您的问题不在删除路线中。在destroy
方法中删除记录后,返回重定向到product.show
路由,这是必需的参数id
。
所以试试
public function destroy($id)
{
$product= Product::where('idoveralRating',$id)
->delete();
return redirect()->route('product.index');
}
回答by Kevin
Ok i want to answer my own question, the problem in my code is that I'm using a default primary key of which is equal to id(in other word, $primaryKey = 'id'), that's why when i pass my $id to destroy it seems wrong because I pass primary key that doesnt belong to the table, that's why the delete method seems not recognized because of you can't delete something that not exist.
好的,我想回答我自己的问题,我的代码中的问题是我使用的默认主键等于 id(换句话说,$primaryKey = 'id'),这就是为什么当我通过 $ id 销毁它似乎是错误的,因为我传递了不属于该表的主键,这就是为什么 delete 方法似乎无法识别的原因,因为您无法删除不存在的内容。
so the problem is solved with simple step change the primary key,protected $primaryKey='idOveralRating'(for my case, and it works!)
所以通过简单的步骤更改主键解决了问题,受保护的 $primaryKey='idOveralRating'(对于我的情况,它有效!)