First, soft delete
Soft deletion must meet the following conditions:
There must be a soft delete field in the table
1.2. Introduce corresponding operations (traits) into the model to realize multiple inheritance
1.3 The identification field specified in the model for soft deletion
The original DELETE delete becomes a soft delete and does not delete real data.
1.4. Create and delete a controller
// Delete the user operation
public function del(int $id) {
/ / soft delete
User::find($id) -> delete();
// Forcible Delete Indicates the actual deletion operation when soft deletion is configured
// User::find($id) -> forceDelete();
return ['status'= >0.'msg'= >'Deleted successfully'];
}
Copy the code
1.5. Added the delete button route
// Delete the user
Route::get('user/del{id}'.'UserController@del') -> name('admin.user.del');
Copy the code
Effect:
2. Restore the user
2.1 Displaying All Users
WithTrashed () displays everything, including those that have been soft deleted
Effect:
2.2 Modifying the Reality template
@if(auth() -> id() ! =$item -> id)
@if($item-> deleted_at ! =null)
<span class="label label-success radius">
<a href="{{route('admin.user.restore'['id'= >$item -> id])}} @else label label-danger radius"> {{route('admin.user.del'['id'= >$item -> id])}} @endif @endifCopy the code
2.3 Route Recovery
// Delete restore
Route::get('user/restore{id}'.'UserController@restore') -> name('admin.user.restore');
Copy the code
2.4 Controller Methods
// Restore the user
public function restore(int $id) {
/ / reduction
User::onlyTrashed() -> where('id'.$id) -> restore();
return redirect(route('admin.user.index')) -> with('success'.'Restore successful');
}
Copy the code
Effect:
3. Batch delete
1. Obtain the ID
@if(auth() -> id() ! =$item ->id)
@if($item -> deleted_at == null)
<input type="checkbox" value="{{$item -> id}}" name="id[]">
@endif
@endif
Copy the code
<a href="javascript:;" onclick="deleteAll()" class="btn btn-danger radius">
<i class="Hui-iconfont"> & #xe6e2; </i> Delete </ in batchesa>
Copy the code
// Generate a token CRSF
const _token = "{{csrf_token()}}";
// Select delete all
function deleteAll() {
/ / about box
layer.confirm('Confirm deletion', {
btn: ['confirm'.'cancel'] / / button
}, function() {
// The selected user
let ids = $('input[name = "id[]"]:checked')
let id = []
$.each(ids, (key, value) => {
id.push($(value).val());
})
// console.log(id)
if (id.length > 0) {
/ / by ajax
$.ajax({
url: "{{route('admin.user.delall')}}",
data: {
id,
_token
},
type: 'DELETE'
}).then((res) => {
console.log(res)
if (res.status == 1) {
layer.msg(res.msg, {
time: 2000,
icon: 1}, () => { location.reload(); })}})}}); }Copy the code
2. Select all to delete the route
// Select delete all
Route::delete('user/delall'.'UserController@delall') -> name('admin.user.delall');
Copy the code
3. Select all controller methods
// Delete all users
public function delall (Request $request) {
$ids = $request -> get('id');
User::destroy($ids);
return ['status'= >1.'msg'= >'All selections deleted successfully'];
}
Copy the code
If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.