【发布时间】:2017-10-06 04:21:27
【问题描述】:
我正在尝试从 Laravel 5.4 中的数据库中删除值。这是我的代码:
UserController.php:
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return redirect()->route('user.index');
}
View.blade.php
<div class="table-responsive">
<table id="datatable-account"
class="table table-vcenter table-hover table-condensed table-bordered text-center">
<thead>
<tr>
<th class="text-center">Username</th>
<th class="text-center">Last Name</th>
<th class="text-center">First Name</th>
<th class="text-center">Role</th>
<th class="text-center">Email</th>
<th class="text-center">Action</th>
</tr>
</thead>
</table>
</div>
<script type="text/javascript">
$('#datatable-account').DataTable({
processing: true,
serverSide: true,
"ajax": '{!! url('api/user') !!}',
"columns": [
{data: 'username', name: 'username'},
{data: 'lastname', name: 'lastname'},
{data: 'firstname', name: 'firstname'},
{data: 'role', name: 'role',
render: function (data, types, full, meta) {
if (data == 0){ return 'Admin' } else return 'User';}
},
{data: 'email', name: 'email'},
{data: 'id', name: "id",
render: function (data, types, full, meta) {
return '<a href="{{ url('delete')}}" data-method="delete" button type="button" class="btn btn-danger btn-xs delete-user" name="delete"><i class="fa fa-times"></i> Delete</button></a>';
}
}
]
});
Routes.php
Route::post('/delete', ['as' => 'delete', 'uses' => 'UserController@destroy']);
当我单击删除按钮时,没有出现错误,但数据库中的值仍然存在。我错过了什么。提前致谢。
【问题讨论】:
-
你遇到了什么错误?
-
不幸的是,我没有收到错误。它只是不执行删除。
-
你试过不用ajax了吗?
-
您似乎有 ajax 错误。初始化删除按钮后尝试签入控制台。
-
你检查过你的ajax方法 POST/GET ,请用 POST 设置它,因为你的 laravel 路由在 POST 中。或者我们有其他解决方案来设置你的 Laravel 路线为“任何”。
标签: laravel laravel-5.4 destroy