通过按钮删除laravel中的行

时间:2018-05-24 09:45:49

标签: php sql laravel

这个问题被问了很多我知道发布的帖子对我有点帮助,我现在有这样的事情。

控制器:

public function destroy($delete)
{
    $delete->delete();
    return redirect()->route('cd.index')->with('success','deleted successfully!');
}

在我看来每行按钮上的类似内容:

@foreach ($scores as $score)
<tr>
    <td>{{ $score->id }}</td>
    <td>{{ $score->serialnumber }}</td>
    <td>{{ $score->name }}</td>
    <td>{{ $score->created_at }}</td>
    <td></td>
    <td>
        <button class="btn btn-danger" type="submit">Delete this Row</button>
        {{ csrf_field() }}
    </td>
</tr>

这是我和我没有得到任何错误的结果,但按钮什么都不做:

Rendered view

2 个答案:

答案 0 :(得分:1)

 <a class="btn btn-sm btn-danger" href="{{route('cd.index',$core->id)}}">
                        <i class="fa fa-trash"></i>&nbsp;Add New Record
                    </a>

public function destroy($delete){
  $data = YourModel::find($delete); 
  $data->delete(); or 

 YourModel::where('id',$delete)->first();
 $data->delete();

 return redirect()->route('cd.index')
                  ->with('success','deleted successfully!');
}

答案 1 :(得分:1)

您的删除应如下所示:

DB::table($tableName)->where('id', $delete)->delete();

$ delete - 记录的ID $ tableName - 存储记录的表的名称

动作:

{{ url('record/delete/' . $score->id) }}

网络路线:

Route::post(record/delete/{id}, 'ControllerName@destroy');

HTML:

<td>
    <form action="..." method="POST">
       {{ csrf_field() }}
       <button class="btn btn-danger" type="submit">Delete this Row</button>

    </form>
</td>

如果您在重新加载页面之前单击它们,也可以删除多个元素

控制器:https://laravel.com/docs/5.6/controllers

功能必须在控制器内(app \ Http \ Controllers)

路线:https://laravel.com/docs/5.6/routing

添加路线到路线文件(routes \ web.php)