Skip to content
Advertisement

How to avoid create record in laravel model creating event

I’m currently using Laravel 8 and I want check a field when my model ‘creating’ event fired. If my field isn’t null, I want cancel creating record. my event is like below:

protected static function booted(){
    static::creating(function ($contactMessage){
        if(request('birthday') != null)
            //Here I want Stop creating record but don't know how!!!
    });
}

Advertisement

Answer

Return false if you want to stop the creation of the record. Model@performInsert will check to see if a false was returned from any event listener for the creating event.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement