Git Product home page Git Product logo

laravel-trash's Introduction

Laravel Trash ๐Ÿ—‘๏ธ

GitHub Actions Latest Version on Packagist Total Downloads License

The Laravel "soft delete" functionality is effectively managed through the use of this package. It works like trash in your system.

Installation โš™๏ธ

  • You can install the package via composer:
composer require ajaycalicut17/laravel-trash
  • Run migrations to create tables for this package:
php artisan migrate
  • Check model is "soft delete" able, this package work by using laravel "soft delete" functionality:
<?php

namespace App\Models;

+ use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
+    use SoftDeletes;
}
  • To enable this package for a model, add the Ajaycalicut17\LaravelTrash\Traits\Trashable trait to the Eloquent model:
<?php

namespace App\Models;

+ use Ajaycalicut17\LaravelTrash\Traits\Trashable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
+    use SoftDeletes, Trashable;
}
  • To start listening "trashed" model event, define a $dispatchesEvents property on your Eloquent model:
<?php

namespace App\Models;

+ use Ajaycalicut17\LaravelTrash\Events\MoveToTrash;
use Ajaycalicut17\LaravelTrash\Traits\Trashable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SoftDeletes, Trashable;
+
+    /**
+     * The event map for the model.
+     *
+     * @var array
+     */
+    protected $dispatchesEvents = [
+        'trashed' => MoveToTrash::class,
+    ];
}
  • To override the trash name (Optional):
<?php

namespace App\Models;

use Ajaycalicut17\LaravelTrash\Events\MoveToTrash;
use Ajaycalicut17\LaravelTrash\Traits\Trashable;
+ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SoftDeletes, Trashable;

    /**
     * The event map for the model.
     *
     * @var array
     */
    protected $dispatchesEvents = [
        'trashed' => MoveToTrash::class,
    ];
+
+    public static function trashName(Model $model): string
+    {
+        return static::class.' '.$model->id;
+    }
}
  • Publishing the config file (Optional):
php artisan vendor:publish --provider="Ajaycalicut17\LaravelTrash\LaravelTrashServiceProvider" --tag="config"
  • To periodically delete the model from the trash, add model:prune Artisan command in your application's App\Console\Kernel class and change the config\trash.php pruning status to "true", you can also enter your pruning period here. (Optional):
+ use Ajaycalicut17\LaravelTrash\Models\Trash;

/**
 * Define the application's command schedule.
 */
protected function schedule(Schedule $schedule): void
{
+    $schedule->command('model:prune', [
+        '--model' => Trash::class,
+    ])->daily();
}

Usage ๐Ÿ”จ

  • To get all trash model data:
Trash::all();
  • To get all trash model and associated model data:
Trash::with('trashable')->get();
  • To restore associated model form trash:
Trash::first()->restoreFromTrash();
  • To delete trashed model and associated model:
Trash::first()->deleteFromTrash();
  • To delete all trashed model and associated model:
Trash::emptyTrash();

Testing ๐Ÿงช

composer test

Changelog ๐Ÿš€

Please see CHANGELOG for more information what has changed recently.

Contributing ๐Ÿค

Please see CONTRIBUTING for details.

Credits ๐Ÿ”ฅ

License ๐Ÿ“ƒ

The MIT License (MIT). Please see License File for more information.

laravel-trash's People

Contributors

ajaycalicut17 avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.