Skip to content

Recycle Bin Behavior

Rory Hellyer edited this page Jun 4, 2017 · 3 revisions

Model

Model needs to use the Illuminate\Database\Eloquent\SoftDeletes trait. Further information on using SoftDeletes trait at https://laravel.com/docs/5.4/eloquent#soft-deleting.

Controller

Add these methods to Controller:

/**
 * Display trashed listings.
 *
 * @return \Illuminate\Http\Response
 */
public function recycle(PersonRecycleDataTable $dataTable)
{
    return $dataTable->render('people.recycle');
}

/**
 * Un-delete the specified resource from the recycle bin.
 *
 * @param  Person $recycledPerson
 * @return JsonResponse
 */
public function restore(Person $recycledPerson)
{
    if ($recycledPerson->restore()) {
        return $this->sendJsonSuccessResponse("You have successfully restored " . $recycledPerson->email . ".");
    } else {
        return $this->sendJsonFailureResponse("Failed to restore " . $recycledPerson->email . ".");
    }
}

/**
 * Permanently delete the specified resource.
 *
 * @param  Person $recycledPerson
 * @return JsonResponse
 */
public function permaDelete(Person $recycledPerson)
{
    if ($recycledPerson->forceDelete()) {
        return $this->sendJsonSuccessResponse("You have permanently deleted " . $recycledPerson->email . ".");
    } else {
        return $this->sendJsonFailureResponse("Failed to permanently delete " . $recycledPerson->email . ".");
    }
}

RouteServiceProvider

Add route binding to ServiceProvider boot() method:

// Person Recycle
Route::bind('recycledPerson', function ($id) {
    return Person::onlyTrashed()->find($id);
});

Routes

Add routes to routes/web.php:

Route::get('/people/recycle', 'PersonController@recycle')->name('people.recycle');
Route::put('/people/recycle/{recycledPerson}', 'PersonController@restore');
Route::delete('/people/recycle/{recycledPerson}', 'PersonController@permaDelete');

Note: Be sure to add these routes ABOVE any resource routes (Route::resource(...)).

RecycleDataTable

Create a new RecycleDataTable that extends DataTable for this resource.

Naming: {resourceName}RecycleDataTable (eg PersonRecycleDataTable)

<?php

namespace App\DataTables;

use WebModularity\LaravelCms\DataTables\RecyclableDataTable;

class PersonRecycleDataTable extends PersonDataTable
{
    use RecyclableDataTable;

    protected $actionView = 'people.actions.recycle';

    protected function getOrder()
    {
        return [[5, "desc"]];
    }

    public function dataTable()
    {
        return $this->recycleDataTable(parent::dataTable());
    }

    public function query()
    {
        return $this->recycleQuery(parent::query());
    }

    protected function getColumns()
    {
        return $this->recycleGetColumns(parent::getColumns());
    }

    protected function getButtons()
    {
        return $this->recycleGetButtons();
    }

    protected function getDrawCallback()
    {
        return $this->recycleGetDrawCallback();
    }
}
Clone this wiki locally