Php 在Laravel 5中不允许删除方法并返回MethodNotAllowedHttpException
所以我可以用表单在我的数据库上创建一个新记录,但不知何故,我无法用表单删除我的数据库,现在,我使用的是Laravel5。 这就是我的代码 routes.phpPhp 在Laravel 5中不允许删除方法并返回MethodNotAllowedHttpException,php,mysql,laravel,laravel-5,blade,Php,Mysql,Laravel,Laravel 5,Blade,所以我可以用表单在我的数据库上创建一个新记录,但不知何故,我无法用表单删除我的数据库,现在,我使用的是Laravel5。 这就是我的代码 routes.php Route::get('/', [ 'as' => '/', 'uses' => 'PagesController@getIndex' ]); Route::resource('product', 'ProductController'); <?php namespace App\Http\Con
Route::get('/', [
'as' => '/',
'uses' => 'PagesController@getIndex'
]);
Route::resource('product', 'ProductController');
<?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');
}
}
@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
@extends('layouts.layout')
@section('Head')
ALL Product
@stop
@section('Body')
@foreach($product as $col)
<h1>{{$col->Name}}</h1>
@endforeach
@stop
ProductController.php
Route::get('/', [
'as' => '/',
'uses' => 'PagesController@getIndex'
]);
Route::resource('product', 'ProductController');
<?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');
}
}
@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
@extends('layouts.layout')
@section('Head')
ALL Product
@stop
@section('Body')
@foreach($product as $col)
<h1>{{$col->Name}}</h1>
@endforeach
@stop
我认为你们的问题不在于删除路径。在destroy
方法中删除记录后,返回重定向到product.show
所需的路由参数id
所以试试看
我认为你们的问题不在于删除路线。在destroy
方法中删除记录后,返回重定向到product.show
所需的路由参数id
所以试试看
表单没有删除方法
你必须这样使用它:
'route'=> array('product.destroy', $product),
在show.blade.php中
<!DOCTYPE html>
<html>
<head>
@yield('Head')
</head>
<body>
@yield('Body')
</body>
</html>
{!!Form::open([
'method' => 'post',
'route'=> array('product.destroy',$product->id),])!!}
{{ method_field('DELETE') }}
...
表单没有删除方法
你必须这样使用它:
'route'=> array('product.destroy', $product),
在show.blade.php中
<!DOCTYPE html>
<html>
<head>
@yield('Head')
</head>
<body>
@yield('Body')
</body>
</html>
{!!Form::open([
'method' => 'post',
'route'=> array('product.destroy',$product->id),])!!}
{{ method_field('DELETE') }}
...
尝试使用“laravel数据绑定”。
在引导方法中将以下代码添加到RouteServiceProvided.php中:
$router->model('Product', Product::class);
并将控制器上的销毁方法更改为:
public function destroy(Product $product)
{
$product->delete();
return back();
}
您在show.blade.php文件中的路径必须如下所示:
'route'=> array('product.destroy', $product),
尝试使用“laravel数据绑定”。
在引导方法中将以下代码添加到RouteServiceProvided.php中:
$router->model('Product', Product::class);
并将控制器上的销毁方法更改为:
public function destroy(Product $product)
{
$product->delete();
return back();
}
您在show.blade.php文件中的路径必须如下所示:
'route'=> array('product.destroy', $product),
好的,我想回答我自己的问题,我代码中的问题是,我使用的默认主键等于id(换句话说,$primaryKey='id'),这就是为什么当我传递$id销毁它时,它看起来是错误的,因为我传递的主键不属于表,这就是为什么delete方法似乎无法识别,因为您无法删除不存在的内容
这样,问题就用简单的步骤解决了
更改主键,protected$primaryKey='idOveralRating'(对于我的情况,它是有效的!)好的,我想回答我自己的问题,代码中的问题是我使用的默认主键等于id(换句话说,$primaryKey='id'),这就是为什么当我传递$id进行销毁时,它似乎是错误的,因为我传递的主键不属于表,这就是为什么delete方法似乎无法识别,因为您无法删除不存在的内容
这样,问题就用简单的步骤解决了
更改主键,protected$primaryKey='idOveralRating'(对于我的情况,它是有效的!)运行php artisan路由:list
,您是否在该列表中看到名为product.destroy
的路由?@AlexeyMezenin运行该命令,它显示存在product.destroy运行php artisan路由:list
,您是否在该列表中看到名为product.destroy
的路由?@AlexeyMezenin运行命令,这表明存在product.Destroy问题是我无法从数据库中删除记录,错误仍然是相同的方法NotallowedHttpException问题是我无法从数据库中删除记录,错误仍然是相同的方法NotallowedHttpException它不起作用,尽管我尝试将其路由到product.show,但它始终路由到product.index。我尝试删除方法字段(和代码中的方法),但它仍然路由到product.indexIt不起作用,尽管我尝试将其路由到product.index,它始终路由到product.index。show,我尝试删除方法字段(和代码中的方法)但它仍然是通往product.index的路线