使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:09:27  来源:igfitidea点击:

Delete row from database table with Laravel 5.2

phplaravellaravel-5

提问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 idinstead 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 getlink but using postroute. Change it to:

您正在创建get链接但使用post路由。将其更改为:

Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

回答by Patryk Uszyński

MethodNotAllowedHttpExceptionmeans that you are trying to access route with bad method. If you use Html::linkRoutethen anchor is generated, but in your routes you have defined Route::post. You need to replace Route::postwith 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::postRoute::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_idin request, but in routes you named your parameter as delete

您试图在这里访问report_id请求,但在路由中您将参数命名为delete