-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
forceDelete() does not work if the model has observer #54733
Comments
Hey there, thanks for reporting this issue. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.
Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
I made a test app, and could not replicate it. Can you share your observer's code? More specifically, any If your observer has a If you want to execute code on your observer after the action is actually performed, use the That would explain why |
Here is my observer code:
|
@exemSK I made the public repo as @crynobone suggested, and still cannot replicate it: https://github.com/rodrigopedra/issue-54733 Please review the code, and either fork and share the link, or send a PR to the repo above - with the changes needed to replicate - so we can review it. You can also search your code base for any listeners of a |
@exemSK This is not a bug but just another corner case of a happy flow from Laravel. your code created it again after deletion /**
* Handle the Demand "deleted" event.
*/
public function deleted( Demand $demand ): void
{ // here $deleted is hydrated but does not exist in db. Try here dd(Demand::query()->find($demand->id)); It will print null
$demand->deleted_by = auth()->user()->id;
$demand->save(); // here you create it again.
$demand->logs()->create([
'details' => [
"text" => 'The demand has been trashed by ' . auth()->user()->name . '.'
]
]);
} fix /**
* Handle the Demand "deleted" event.
*/
public function deleting( Demand $demand ): void
{
$demand->deleted_by = auth()->user()->id;
}
/**
* Handle the Demand "deleted" event.
*/
public function deleted( Demand $demand ): void
{
$demand->logs()->create([
'details' => [
"text" => 'The demand has been trashed by ' . auth()->user()->name . '.'
]
]);
}
/**
* Handle the Demand "deleted" event.
*/
public function forceDeleted( Demand $demand ): void
{
$demand->logs()->create([
'details' => [
"text" => 'The demand has been forceDeleted by ' . auth()->user()->name . '.'
]
]);
} |
@macropay-solutions good catch! Alternative fix if the public function deleted(Demand $demand): void
{
// `Model@isForceDeleting()` is only available if the `Demand` model
// uses the `SoftDeletes` trait
if (! $demand->isForceDeleting())
{
// additional logic
$demand->deleted_by = auth()->user()->id;
$demand->save();
}
// this might fail if the `logs` relation has a foreign key
// as the force deleted record will no longer be in the database
$demand->logs()->create([
'details' => [
"text" => 'The demand has been trashed by ' . auth()->user()->name . '.',
],
]);
} |
@rodrigopedra We updated the original response with a fix. Your proposal makes an extra save. |
Ok, thanks for your time and response. My mistake, I mistakenly assumed that if the model uses the |
@exemSK you can close this issue. |
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:
Controller:
Firing the deletePermanently() function return 'success', but the model is not deleted and created_at timestamp IS UPDATED.
The text was updated successfully, but these errors were encountered: