使用 Laravel 5.2 从数据库表中删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41715336/
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 row from database table with Laravel 5.2
提问by VLS
Recently I've started with Laravel 5.2 and I'm trying to make delete button which will delete row from database. Very basic and trivial but seems I can't make it.
最近我开始使用 Laravel 5.2,我正在尝试制作删除按钮,该按钮将从数据库中删除行。非常基本和琐碎,但似乎我做不到。
I'm following documentation for delete: https://laravel.com/docs/5.2/queries#deletes
我正在关注删除文档:https: //laravel.com/docs/5.2/queries#deletes
And I have made this. My route:
我已经做到了。我的路线:
Route::post('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');
Button in the view
视图中的按钮
{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}
and the controller
和控制器
public function destroy(Request $request){
$report = $request['report_id'];
Report::find($report);
$report->delete();
$request->session()->flash('alert-success', ' Report is deleted successfully.');
return redirect()->route('admin.flags');
}
I've tried solutions from other threads but I always got error:
我尝试过其他线程的解决方案,但总是出错:
MethodNotAllowedHttpException in compiled.php line 8936:
在compiled.php第8936行中的MethodNotAllowedHttpException:
New error:
新错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reports.id' in 'where clause' (SQL: select * from `reports` where `reports`.`id` is null limit 1
Why is searching for id
instead of report_id
?
为什么是搜索id
而不是report_id
?
UPDATE:
更新:
button
按钮
{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}
Controller
控制器
public function destroy(Request $request){
$report = $request['report_id'];
dd( $request->input('delete'));
Report::where('report_id', $report)->first();
$report->delete();
$request->session()->flash('alert-success', ' Report is deleted successfully.');
return redirect()->route('admin.flags');
}
Route
路线
Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');
Update 2: This seems to work but is it secure enough? view:
更新 2:这似乎有效,但是否足够安全?看法:
{!! Form::open(array('route' => array('admin.flags.destroy', $flag->report_id), 'method' => 'get')) !!}
<button type="submit">Delete</button>
{!! Form::close() !!}</td>
Controller
控制器
public function destroy($report_id){
Report::destroy($report_id);
//$request->session()->flash('alert-success', ' Report is deleted successfully.');
return redirect()->route('admin.flags');
}
采纳答案by AddWeb Solution Pvt Ltd
I think your code need to update like:
我认为您的代码需要更新如下:
public function destroy($delete){
$report = $delete;
$rsltDelRec = Report::find($report);
$rsltDelRec->delete();
$request->session()->flash('alert-success', ' Report is deleted successfully.');
return redirect()->route('admin.flags');
}
Hope this work for you!
希望这对你有用!
回答by Mevlüt?zdemir
Try this:
尝试这个:
Controller:
控制器:
public function destroy(Report $report){
$report->delete();
return redirect()->route('admin.flags');
}
回答by Alexey Mezenin
You're creating get
link but using post
route. Change it to:
您正在创建get
链接但使用post
路由。将其更改为:
Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');
回答by Patryk Uszyński
MethodNotAllowedHttpException
means that you are trying to access route with bad method. If you use Html::linkRoute
then anchor is generated, but in your routes you have defined Route::post
. You need to replace Route::post
with Route::get
. But if you want to make it safer you need to create simple form with delete button and CSRF token.
MethodNotAllowedHttpException
意味着您正在尝试使用错误的方法访问路由。如果您使用,Html::linkRoute
则会生成锚点,但在您的路线中您已经定义了Route::post
. 您需要替换Route::post
为Route::get
. 但是如果你想让它更安全,你需要创建带有删除按钮和 CSRF 令牌的简单表单。
<form method="POST" action="{{ URL::route('admin.flags.destroy', {'delete' => $flag->report_id}) }}">
{{ csrf_field() }}
<!-- submit button -->
</form>
Why is searching for id instead of report_id?
为什么要搜索 id 而不是 report_id?
You need to replace
你需要更换
Report::find($report);
with
和
$report = Report::where('report_id', $report)->first();
public function destroy(Request $request){
$report = $request['report_id'];
....
You are trying here to access report_id
in request, but in routes you named your parameter as delete
您试图在这里访问report_id
请求,但在路由中您将参数命名为delete