Skip to content

forceDelete() does not work if the model has observer #54733

@exemSK

Description

@exemSK

Laravel Version

11.31

PHP Version

8.4.3

Database Driver & Version

No response

Description

The forceDelete() method does not delete a model when it has an observer. The method returns true and updates the model's created_at timestamp to the current date and time. You must use forceDeleteQuietly() to delete a model.

Steps To Reproduce

Model:

#[ ObservedBy( [ DemandObserver::class ] ) ]
class Demand extends Model
{
    use SoftDeletes;

    ......
}

Controller:


public function deletePermanently( Demand $demand )
    {
        if ( auth()->user()->can(  'forceDelete',  $demand  )  ){

            $deleted = $demand->forceDelete();

            return $deleted ? back()->with(  'success', 'The demand has been permanently removed.' ) : back()->withErrors( [ 'error' => 'The demand was not deleted.' ] );
        }

        return back()->withErrors( [ 'error' => 'You do not have permission to perform this operation.' ] );
    }

Firing the deletePermanently() function return 'success', but the model is not deleted and created_at timestamp IS UPDATED.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions