Data soft delete

Refer to the Laravel documentation first to implement the model’s soft delete:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. class Post extends Model
  6. {
  7. use SoftDeletes;
  8. }

This way, the data displayed in the grid list is all undeleted data.

  1. return Grid::make(new Post(), function (Grid $grid) {
  2. $grid->id('ID')->sortable();
  3. $grid->title('Title');
  4. $grid->created_at('Created at');
  5. $grid->updated_at('Updated at');
  6. });

Recycle Bin Entrance

Next, we need to add an entry point that will allow us to see the soft-deleted data, which can be done using model-grid‘s scope filter.

  1. $grid->filter(function () {
  2. // Range filter, calling the model's `onlyTrashed` method to query out the soft-deleted data.
  3. $filter->scope('trashed', 'recycle bin')->onlyTrashed();
  4. });

A Recycle Bin button will appear in the drop-down menu of the filter button in the table header, and clicking it will call the model’s onlyTrashed method to look up the deleted data from the table, i.e., the data in the Recycle Bin.

Data soft delete - 图1

Row recovery operations

By following this method, we can add a recovery action to each row of data in the Recycle Bin for easy data recovery

Define the operation class app/Admin/Actions/Post/Restore.php first.

  1. <?php
  2. namespace App\Admin\Actions\Post;
  3. use Dcat\Admin\Grid\RowAction;
  4. use Illuminate\Http\Request;
  5. class Restore extends RowAction
  6. {
  7. protected $title = 'Restore';
  8. protected $model;
  9. // Note that the construct method parameters must have default values
  10. public function __construct(string $model = null)
  11. {
  12. $this->model = $model;
  13. }
  14. public function handle(Request $request)
  15. {
  16. $key = $this->getKey();
  17. $model = $request->get('model');
  18. $model::withTrashed()->findOrFail($key)->restore();
  19. return $this->response()->success('Restored')->refresh();
  20. }
  21. public function confirm()
  22. {
  23. return ['Are you sure?'];
  24. }
  25. public function parameters()
  26. {
  27. return [
  28. 'model' => $this->model,
  29. ];
  30. }
  31. }

Add to Row Action:

  1. use App\Models\Post;
  2. use App\Admin\Actions\Post\Restore;
  3. $grid->actions(function (Grid\Displayers\Actions $actions) {
  4. if (request('_scope_') == 'trashed') {
  5. $actions->append(new Restore(Post::class));
  6. }
  7. });

Batch recovery operations

Define the operation class app/Admin/Actions/Post/BatchRestore.php first:

  1. <?php
  2. namespace App\Admin\Actions\Post;
  3. use Dcat\Admin\Grid\BatchAction;
  4. use Illuminate\Http\Request;
  5. class BatchRestore extends BatchAction
  6. {
  7. protected $title = 'Restore';
  8. protected $model;
  9. // Note that the construct method parameters must have default values
  10. public function __construct(string $model = null)
  11. {
  12. $this->model = $model;
  13. }
  14. public function handle(Request $request)
  15. {
  16. $model = $request->get('model');
  17. foreach ((array) $this->getKey() as $key) {
  18. $model::withTrashed()->findOrFail($key)->restore();
  19. }
  20. return $this->response()->success('Restored')->refresh();
  21. }
  22. public function confirm()
  23. {
  24. return ['Are you sure?'];
  25. }
  26. public function parameters()
  27. {
  28. return [
  29. 'model' => $this->model,
  30. ];
  31. }
  32. }

Add to batch operation:

  1. use App\Models\Post;
  2. use App\Admin\Actions\Post\BatchRestore;
  3. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  4. if (request('_scope_') == 'trashed') {
  5. $batch->add(new BatchRestore(Post::class));
  6. }
  7. });