All Projects → f9webltd → Laravel Deletable

f9webltd / Laravel Deletable

Licence: mit
👾 Gracefully restrict deletion of Laravel Eloquent models

Projects that are alternatives of or similar to Laravel Deletable

Eloquent Approval
Approval process for Laravel Eloquent models
Stars: ✭ 79 (-42.34%)
Mutual labels:  eloquent, eloquent-models, laravel, laravel-package
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (-43.07%)
Mutual labels:  eloquent, eloquent-models, laravel, laravel-package
Laravel Cascade Soft Deletes
Cascading deletes for Eloquent models that implement soft deletes
Stars: ✭ 498 (+263.5%)
Mutual labels:  eloquent, eloquent-models, laravel
Simple Cache
An easy to use Caching trait for Laravel's Eloquent Models.
Stars: ✭ 19 (-86.13%)
Mutual labels:  eloquent, laravel, laravel-package
Elasticquent
Maps Laravel Eloquent models to Elasticsearch types
Stars: ✭ 1,172 (+755.47%)
Mutual labels:  eloquent, eloquent-models, laravel
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+44.53%)
Mutual labels:  eloquent, laravel, laravel-package
Eloquent Viewable
Associate views with Eloquent models in Laravel
Stars: ✭ 404 (+194.89%)
Mutual labels:  eloquent-models, laravel, laravel-package
Eloquentfilter
An Eloquent Way To Filter Laravel Models And Their Relationships
Stars: ✭ 1,113 (+712.41%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (+73.72%)
Mutual labels:  eloquent, eloquent-models, laravel
Laravel Nullable Fields
Handles saving empty fields as null for Eloquent models
Stars: ✭ 88 (-35.77%)
Mutual labels:  eloquent, eloquent-models, laravel
Eloquent Hashids
On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚡ Fast)
Stars: ✭ 196 (+43.07%)
Mutual labels:  eloquent, eloquent-models, laravel
Eloquent Relativity
Allows you to decouple your eloquent models from one another.
Stars: ✭ 112 (-18.25%)
Mutual labels:  eloquent, eloquent-models, laravel-package
Eloquent Ldap
A Laravel 5.1 package that first tries to log the user against the internal database if that fails, it tries against the configured LDAP/AD server.
Stars: ✭ 19 (-86.13%)
Mutual labels:  eloquent, laravel, laravel-package
Laravel Lucene Search
Laravel 4.2, 5.* package for full-text search over Eloquent models based on ZF2 Lucene.
Stars: ✭ 75 (-45.26%)
Mutual labels:  eloquent, eloquent-models, laravel
Elasticsearch Eloquent
⚡️ Eloquent models for Elasticsearch.
Stars: ✭ 100 (-27.01%)
Mutual labels:  eloquent-models, laravel, laravel-package
Searchable
Search/filter functionality for Laravel's Eloquent models
Stars: ✭ 113 (-17.52%)
Mutual labels:  eloquent, eloquent-models, laravel
Pagination
🎁 Laravel 5 Custom Pagination Presenter
Stars: ✭ 119 (-13.14%)
Mutual labels:  laravel, laravel-package
Laravel Natural Language
This package makes using the Google Natural API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
Stars: ✭ 119 (-13.14%)
Mutual labels:  laravel, laravel-package
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (-11.68%)
Mutual labels:  eloquent, laravel
Sieve
A simple, clean and elegant way to filter Eloquent models.
Stars: ✭ 123 (-10.22%)
Mutual labels:  eloquent, laravel

Packagist Version Scrutinizer coverage (GitHub/BitBucket) Scrutinizer code quality (GitHub/Bitbucket) Travis (.org) StyleCI Status Packagist License

Laravel Deletable

Gracefully handle deletion restrictions on your Eloquent models, as featured on Laravel News

Requirements

PHP >= 7.2, Laravel >= 5.8.

PHP 8.0 is supported for Laravel >= 6x

Installation

composer require f9webltd/laravel-deletable

The package will automatically register itself.

Optionally publish the configuration file by running: php artisan vendor:publish and selecting the appropriate package.

Documentation

Usage

Within an Eloquent model use the RestrictsDeletion trait:

namespace App;

use F9Web\LaravelDeletable\Traits\RestrictsDeletion;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
   use RestrictsDeletion;
}

The trait overrides calls to Eloquent's delete() method.

Implement the isDeletable() method within the model in question.

This method should return true to allow deletion and false to deny deletion:

namespace App;

use F9Web\LaravelDeletable\Traits\RestrictsDeletion;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
  use RestrictsDeletion;
  
  public function isDeletable() : bool
  {
    return $this->orders()->doesntExist();
  }  
}

The above denies deletion of users with orders.

None deletable models throw an exception when the isDeletable() method returns false:

namespace App\Controllers;

use F9Web\LaravelDeletable\Exceptions\NoneDeletableModel;
use App\User;

class UsersController
{
  public function destroy(User $user) : bool
  {
    try {
      $user->delete();
    } catch (NoneDeletableModel $e) {
      // dd($ex->getMessage());
    }
  }  
}

Eloquent Base Model

As the default isDeletable() method returns true, a base Eloquent model can be optionally defined from which all models extend. Each model can then optionally implement the isDeletable() method as needed.

Customising Messages

The default exception message is defined within the config f9web-laravel-deletable.messages.default and is simply The model cannot be deleted.

By setting f9web-laravel-deletable.messages.default to null a more detailed message is automatically generated i.e. Restricted deletion: App\User - 1 is not deletable.

Custom messages can be set within the isDeletable() method:

namespace App;

use F9Web\LaravelDeletable\Traits\RestrictsDeletion;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class User extends Model
{
  use RestrictsDeletion;
  
  public function isDeletable() : bool
  {
    if (Str::endsWith($this->email, 'f9web.co.uk')) {
        return $this->denyDeletionReason('Users with f9web.co.uk company email addresses cannot be deleted');
    }

    return true;
  }  
}

The denyDeletionReason() method can be used to specify the exception message.

In the above case, the exception message is Users with f9web.co.uk company email addresses cannot be deleted.

Multiple Checks

Multiple checks can be performed within isDeletable() if necessary, each of which returning a different exception message:

namespace App;

use F9Web\LaravelDeletable\Traits\RestrictsDeletion;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class User extends Model
{
  use RestrictsDeletion;
  
  public function isDeletable() : bool
  {
    if (Str::endsWith($this->email, 'f9web.co.uk')) {
       return $this->denyDeletionReason('Users with f9web.co.uk company email addresses cannot be deleted');
    }
    
    if ($this->orders->isNotEmpty()) {
       return false;
    }
    
    if ($this->purchaseOrders->isNotEmpty()) {
       return $this->denyDeletionReason('This user has active purchase orders and cannot be deleted');
    }
    
    if ($this->overdueInvoices->isNotEmpty()) {
       return $this->denyDeletionReason('Users with overdue invoices cannot be deleted');
    }

    return true;
  }  
}

Contribution

Any ideas are welcome. Feel free to submit any issues or pull requests.

Testing

composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].