使用Laravel 5.2从数据库表中删除行

6

最近我开始学习Laravel 5.2,并尝试创建一个删除按钮,用于从数据库中删除行。看起来很基础和琐碎,但似乎我做不到。

我正在按照删除文档进行操作:https://laravel.com/docs/5.2/queries#deletes

我已经建立了我的路由:

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

视图中的按钮

{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}

以及控制器

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');
}

我尝试了其他帖子中的解决方案,但总是出现错误:

在compiled.php的第8936行中出现MethodNotAllowedHttpException异常:

新错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reports.id' in 'where clause' (SQL: select * from `reports` where `reports`.`id` is null limit 1

为什么要搜索 id 而不是 report_id
更新:
按钮
{!! Html::linkRoute('admin.flags.destroy', 'Delete', $flag->report_id) !!}

控制器

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::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

更新2:这似乎可行,但安全性如何? 查看:
 {!! Form::open(array('route' => array('admin.flags.destroy', $flag->report_id), 'method' => 'get')) !!}
        <button type="submit">Delete</button>
 {!! Form::close() !!}</td> 

控制器

public function destroy($report_id){

  Report::destroy($report_id);
  //$request->session()->flash('alert-success', ' Report is deleted successfully.');

  return redirect()->route('admin.flags');
}
4个回答

5
我认为您的代码需要更新,就像这样:
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');
}

希望这对您有用!

谢谢你的回答。出现了Call to a member function delete() on string的错误。 - VLS
dd($report); 返回 "1",即字符串。 - VLS
@VLS请检查我的答案。我进行了更正。 - AddWeb Solution Pvt Ltd

2

请尝试以下内容:

控制器:

  public function destroy(Report $report){

          $report->delete();

          return redirect()->route('admin.flags');

    }

1
你正在创建 `get` 链接,但是使用了 `post` 路由。请将其更改为:
Route::get('flags/destroy/{delete}', 'FlagsController@destroy')->name('admin.flags.destroy');

使用reports_id而不是reports.id来检查您的查询。 - Bugfixer
我正在使用它。请检查我的问题中的函数和查询。它正在请求报告ID。 - VLS
1
检查你的模型中的主键。应该像这样保护$primaryKey = 'report_id'; // find() 使用主键 // 看起来你有 $primaryKey = 'id' - Bugfixer

1
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>

为什么要搜索ID而不是报告ID?
您需要替换。
Report::find($report);

带有。
$report = Report::where('report_id', $report)->first();

public function destroy(Request $request){

    $report = $request['report_id'];
    ....

你在尝试访问请求中的report_id,但在路由中将参数命名为delete

但是你将模型分配给变量的方式是这样的:$report = Report::where('report_id', $report)->first();。之前你只有 Report::find... 没有其他东西。 - Patryk Uszyński
首先,不要在URL中使用GET方法,否则任何人都可以通过更改参数来删除所有记录。请使用某种身份验证来验证用户。 - Bugfixer
仍然出现相同的错误 Call to a member function delete() on null .. 当我点击删除时,URL 是 ..flags/destroy/1 - VLS
尝试使用 dd($request->input('delete')); - Patryk Uszyński
1
但是,能够向您发送邮件的人无法预测您的令牌。它们是唯一的,并与您当前的会话相关联。 - Patryk Uszyński
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接