f9webltd / laravel-deletable Goto Github PK
View Code? Open in Web Editor NEW:space_invader: Gracefully restrict deletion of Laravel Eloquent models
License: MIT License
:space_invader: Gracefully restrict deletion of Laravel Eloquent models
License: MIT License
Implement method to disable deletion checks, i.e. so the result of isDeletable()
is always true and any checks within isDeletable()
are skipped. The API would look something along the lines of: the following.
A user may have a "deletable" restriction:
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();
}
}
Assuming the user had orders, the following would fail and the model would not be deleted:
$user->delete();
The following would allow the deletable check to be skipped and the model would be deleted:
$user->withoutDeletableRestrictions()->delete()
Hello,
Can you update the composer.json to support Laravel 8?
Ads a simple trait called NoneDeletableModel
to the package. The trait could then be used in Eloquent models to deny all deletion attempts:
declare(strict_types=1);
namespace F9Web\LaravelDeletable\Traits\Traits;
use F9Web\LaravelDeletable\Traits\RestrictsDeletion;
trait NoneDeletableModel
{
use RestrictsDeletion;
public function isDeletable() : bool
{
return false;
}
}
To prevent all deletion requests for a model:
declare(strict_types=1);
namespace App\Models;
use F9Web\LaravelDeletable\Traits\NoneDeletableModel;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use NoneDeletableModel;
}
Now, orders cannot be deleted.
It would be nice to have support of mass delete, e.g. User::where('is_archived', true)->delete()
. Something like isMassDeletable(): bool
. Laravel SoftDeletes
Trait supports mass delete, so its code may be helpful.
PHP 8 compatibility isn't included in the composer.json.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.