如果在laravel 5.4中从表中删除记录,如何保持在同一页面上?

时间:2019-05-13 07:41:19

标签: php laravel-5.4

我的数据库表中有多个记录现在,当我单击特定记录删除按钮时想要的内容将被删除,并且该页面将保留在同一页面上而无需任何重定向。那么,我该怎么做?请帮助我。

控制器:UserController.php

public function delete_emp($id)
{
    DB::delete('delete from emp where id = ?',[$id]);
}

视图:emp-list.blade.php

<table class="table">
    <thead>
        <tr>
            <th>Full Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach($result as $row)
        <tr>
            <td>{{ $row->fname }}</td>
            <td>{{ $row->email }}</td>
            <td>{{ $row->phone }}</td>
            <td>
                <a href="{{url('/delete_emp')}}/{{ $row->id }}" class="btn btn-danger" onclick="return confirm('are you sure')">Delete</a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

route / web.php

Route::get('delete_emp/{id}','UserController@delete_emp');

1 个答案:

答案 0 :(得分:0)

在UserController.php中,只需在查询下方添加一行

public function delete_emp($id)
{
    DB::delete('delete from emp where id = ?',[$id]);
    return redirect()->back();
}