Cancelling a model save / update / delete through events

Submitted by stevebauman - 3 years ago

When registering a model observer using `Model::observer(Observer::class)`, you can return false and laravel will cancel the method.

class ModelObserver
{
    public function creating(Model $model)
    {
        return true;    
    }
    
    public function deleting(Model $model)
    {
        return false;
    }
    
    public function updating(Model $model)
    {
        return false;
    }
}

// In another file

$model->delete();

// This model will never be able to be updated or deleted, however it can be created.