Laravel Deletable Trait

Laravel Deletable Trait

Laravel Deletable is a package to handle deletion restrictions to your Eloquent models gracefully. The package provides a RestrictsDeletion trait for models you’d like to control 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->isEmpty();
    }
}

When a model’s isDeletable() method returns false, a model exception occurs:

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($e->getMessage());
        }
    }
}

You can learn more about this package, get full installation instructions, and view the source code on GitHub at f9webltd/laravel-deletable.

Filed in: News
Laravel News Partners

Laravel Jobs

Laravel Developer
Remote
Enjin
Senior Laravel Backend Engineer
Remote, USA Only
Kittyhawk.io, Inc.
Senior Laravel Developer
Remote, EU timezones only
Mindbeat
Experienced Laravel Developer
Remote
SolidProfessor
Full Time ad Remote - Experienced Laravel Developer
Europe Only
Thunderbite

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.