Laravel HTML表单的删除按钮 [英] Laravel delete button with HTML form
问题描述
我使用的是HTML表单,而不是Laravel Collective.
I'm using the HTML form, not Laravel Collective.
到目前为止,我已经为CMS中的用户成功创建了CRUD,但有一件事情困扰我:
For now I've successfully created a CRUD for a users in my CMS, but one thing bothers me:
如何在用户列表中而不是特定的编辑页面上设置删除"按钮?
How can I set a Delete button in my list of users, instead of the specific edit page?
此外,当用户单击删除"按钮以显示用于删除特定用户的确认弹出窗口时,也会很好.
Also, it will be nice when a user clicks on the Delete button to show up confirmation popup for deleting the specific user.
所以,这是我的代码:
控制器:
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return redirect('/admin/users');
}
用户列表页面:
@extends('layouts.backend')
@section('content')
<h1>Users</h1>
<a class="btn btn-primary" href="/admin/users/create">Create new user</a>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
@if($users)
@foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role ? $user->role->name : 'User has no role'}}</td>
<td>{{$user->status == 1 ? 'Active' : 'Not active'}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
<td>{{$user->updated_at->diffForHumans()}}</td>
<td>
<a href="/admin/users/{{$user->id}}/edit" class="btn btn-primary">Edit</a>
<a class="btn btn-danger" href="">Delete</a> // HOW TO ACHIEVE THIS?
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
@endsection
特定的编辑用户页面:
@extends('layouts.backend')
@section('content')
<h1>Edit user</h1>
<form method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" value="{{$user->name}}">
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" name="email" class="form-control" value="{{$user->email}}">
</div>
<div class="form-group">
<label>Role:</label>
<select name="role_id" class="form-control">
@if($user->role_id == 1)
<option value="1" selected>Administrator</option>
<option value="2">Editor</option>
@else
<option value="1">Administrator</option>
<option value="2" selected>Editor</option>
@endif
</select>
</div>
<div class="form-group">
<label>Status:</label>
<select name="status" class="form-control">
@if($user->status == 1)
<option value="1" selected>Active</option>
<option value="0">Not active</option>
@else
<option value="1">Active</option>
<option value="0" selected>Not active</option>
@endif
</select>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" value="{{$user->password}}">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Update user" class="btn btn-primary">
</div>
</form>
<form id="delete-form" method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<input type="submit" class="btn btn-danger" value="Delete user">
</div>
</form>
@include('inc.errors')
@endsection
路线:
Route::group(['middleware'=>'admin'], function(){
Route::resource('admin/users', 'AdminUsersController');
Route::get('/admin', function(){
return view('admin.index');
});
// Route::resource('admin/posts', 'AdminPostsController');
});
推荐答案
从发布的代码来看,这不是很明显,但是您的DELETE
路由需要DELETE
方法.应当!
It's not obvious from the code you posted, but your DELETE
route expects DELETE
method. As it should!
但是在您的列表中,您尝试使用GET
方法访问它.
But on your list you're trying to access it with GET
method.
真的,您应该只重复使用编辑页面中的代码,而该页面已经伪造了DELETE
方法.
Really you should just reuse the code from the edit page, which fakes DELETE
method already.
类似这样的东西:
...
<td>
<a href="/admin/users/{{$user->id}}/edit" class="btn btn-primary">Edit</a>
<form method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<input type="submit" class="btn btn-danger delete-user" value="Delete user">
</div>
</form>
</td>
...
...
// Mayank Pandeyz's solution for confirmation customized for this implementation
<script>
$('.delete-user').click(function(e){
e.preventDefault() // Don't post the form, unless confirmed
if (confirm('Are you sure?')) {
// Post the form
$(e.target).closest('form').submit() // Post the surrounding form
}
});
</script>
这篇关于Laravel HTML表单的删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!