My Blog Posts

Laravel Deletable Pack

Laravel Deletable Pack


Laravel Deletable is a package that imposes deletion restrictions on your Eloquent models. Package RestrictsDeletionprovides a feature for the models you want to control deleting:

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

A model isDeletable()when the method returns Falseconsists of a model exception:

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 all installation instructions, and review the source code on GitHub at f9webltd / laravel-deletable.



Share this article


Comments (0)

Comment