Skip to content

[12.x] isSoftDeletable(), isPrunable(), and isMassPrunable() to model class #56060

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

Merged
merged 6 commits into from
Jun 17, 2025

Conversation

shaedrich
Copy link
Contributor

@shaedrich shaedrich commented Jun 17, 2025

Before

if (in_array(MassPrunable::class, class_uses_recursive($myClass))) {
    // …
}

After

if ($myClass::isSoftDeletable()) {
    // …
}

Benefits

✅ Encapsulated implementation details: Unrelated places don't need to concern themselves with how it's done
✅ Single source of truth: Now easily refactorable, only one place needs to be changed
✅ DRY

Alternatives

  • Property hook

*/
protected function isPrunable(): bool
{
return in_array(Prunable::class, class_uses_recursive(static::class)) || static::isMassPrunable();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: It would be great if there were a ClassUsesRecursive once helper that would store these values for the lifetime of a request.

Written out as you have here, we can see that we're going to loop through all the traits of the class and its parent-classes twice if the class is MassPrunable but not Prunable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍🏻 I had a similar idea—looks like there's an appetite for this 🚀

@taylorotwell taylorotwell merged commit 1cefdac into laravel:12.x Jun 17, 2025
47 of 60 checks passed
@shaedrich shaedrich deleted the soft-deletes-method branch June 17, 2025 17:01
taylorotwell added a commit to illuminate/database that referenced this pull request Jun 17, 2025
… class (#56060)

* feat(database): ✨ Add isSoftDeletable(), isPrunable(), and isMassPrunable() to model class

* fix(database): 🐛 Call model method instead of command method

* Restore "only check for soft deletes once when mass-pruning"

Co-authored-by: Luke Kuzmish <[email protected]>

See laravel/framework#55274

See laravel/framework#56060 (comment)

* Fix typo

Follow-up to 07520f2225a78a183c6d02da11f662ddf207657d

* formatting

* fix test

---------

Co-authored-by: Taylor Otwell <[email protected]>
taylorotwell added a commit to illuminate/routing that referenced this pull request Jun 17, 2025
… class (#56060)

* feat(database): ✨ Add isSoftDeletable(), isPrunable(), and isMassPrunable() to model class

* fix(database): 🐛 Call model method instead of command method

* Restore "only check for soft deletes once when mass-pruning"

Co-authored-by: Luke Kuzmish <[email protected]>

See laravel/framework#55274

See laravel/framework#56060 (comment)

* Fix typo

Follow-up to 07520f2225a78a183c6d02da11f662ddf207657d

* formatting

* fix test

---------

Co-authored-by: Taylor Otwell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants