[英]delete method not allowed and returning MethodNotAllowedHttpException in Laravel 5
因此,我可以使用表單在數據庫上創建新記錄,但是由於某種原因,我無法使用表單刪除數據庫。現在,我正在使用laravel5。這就是我的代碼
routes.php
Route::get('/', [
'as' => '/',
'uses' => 'PagesController@getIndex'
]);
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('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
@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刪除數據庫(假設我輸入product / 1),這意味着我想刪除ID為1的數據庫。
到目前為止,我已經實現了基於輸入的ID來顯示數據庫的功能,但是當我想將ID路由到ProductController類中的destroy方法時,它表明method =>'delete'不允許,我做錯了什么?
嘗試使用“ 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),
FORM沒有DELETE方法。
您必須像這樣使用它:
在你的show.blade.php中
{!!Form::open([
'method' => 'post',
'route'=> array('product.destroy',$product->id),])!!}
{{ method_field('DELETE') }}
...
我認為您的問題不在刪除路由中。 您刪除記錄后destroy
方法,返回重定向到product.show
這是必需的參數路徑id
。
所以嘗試
public function destroy($id)
{
$product= Product::where('idoveralRating',$id)
->delete();
return redirect()->route('product.index');
}
好的,我想回答我自己的問題,代碼中的問題是我使用的默認主鍵等於id(換句話說,$ primaryKey ='id'),這就是為什么我傳遞$時的原因id銷毀它似乎是錯誤的,因為我傳遞了不屬於該表的主鍵,這就是為什么delete方法似乎無法識別的原因,因為您無法刪除不存在的東西。
因此問題可以通過簡單的步驟更改主鍵,保護$ primaryKey ='idOveralRating'來解決(對於我而言,它可以正常工作!)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.