Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

How to prevent a model from saving using Laravel events

1
by mjauvin, last modified on November 29th, 2022

In the boot() method of your plugin, add this snippet:

Event::listen('eloquent.saving: System\Models\RequestLog', function ($model) {
    if (str_ends_with($model->url, '/bad-url')) {
        return false;
    }   
});

or the preferred method:

System\Models\RequestLog::saving(function ($model) {
    if (str_ends_with($model->url, '/bad-url')) {
        return false;
    }
}); 

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?