Repository navigation
Conversation
Add #[model(soft_deletes)], which makes delete set a deleted_at column instead of removing the row. Model queries, relationship queries, eager loads, counts, and has filters leave out soft-deleted rows, and with_trashed and only_trashed opt back in. Models gain trashed, restore, and force_delete, and model queries gain restore and force_delete. restore runs a new restoring hook and dispatches a Restored event.
Keep the model's updated_at in step with the row when soft deleting and restoring, as Eloquent's runSoftDelete and restore do, and let a query's force_delete also remove trashed rows, as Eloquent's Builder::forceDelete does.
|
🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Soft deletes are one of the most widely used Eloquent features, and #217 notes they were planned for a later slice of
laravel::model. This PR adds them, following the shape of the existing model API as closely as possible. If you already have a different design in mind, please feel free to close this. I'm happy to rework it in whatever direction you prefer.The behavior follows
SoftDeletesandSoftDeletingScopeon Laravel13.x.What changes
#[model(soft_deletes)]is opt-in, so models without it behave exactly as before. It requires adeleted_at: Option<Timestamp>field (a compile error says so otherwise), and it implements a newSoftDeletesmarker trait.with_trashed,only_trashed, and the queryrestore/force_deleteare only available onBuilder<M: SoftDeletes>, so calling them on a model without soft deletes fails to compile, with anon_unimplementedhint.__query::<M>(). Every model query goes through it, so relationship queries, eager loads,with_countand other aggregates,hasfilters, and route model binding all leave out trashed rows without touching each relation type. The condition is table-qualified (posts.deleted_at is null), so it stays unambiguous in joins. Self-relation subqueries alias the table through the existingRequalifypass, which should rewrite the qualified column too, but that path isn't covered by a dedicated test yet.with_trashedremoves the scope by recognizing a private marker carried in the condition's existingConditionKind::Typedslot. No newConditionKindvariant or compiler change was needed.query_of(used byupdate!,save,refresh,fresh, anddelete) drops the scope, matching Eloquent'snewQueryWithoutScopesbehavior.deleteon a soft-deleting model, and on its queries, setsdeleted_atandupdated_at. On a model instance, both fields are also updated in memory with the same timestamp written to the row, as in Eloquent'srunSoftDelete. It still runsdeletingand dispatchesDeleted, and the event's model reportstrashed() == true.restoreclearsdeleted_atand touchesupdated_atin the database and on the model. It runs a newHooks::restoringhook, which has a default no-op body so existingHooksimpls are unaffected, and dispatches a newRestored<M>event. A query'srestoreincludes trashed rows, as Eloquent's does.force_deleteon a model runsdeletingand dispatchesDeleted. On a query it ignores the scope and also removes matching rows that are already trashed, matching Eloquent'sBuilder::forceDelete(), which calls$this->query->delete()on the base query without global scopes (unchanged in 13.x).Metagainsdeleted_at: Option<SoftDelete<M>>.#[model]is its only constructor in the codebase.Docs
docs/models.mdgains a "Soft deleting" section with "Querying soft-deleted models" and "Restoring and permanently deleting" subsections. The hooks table gainsrestoring, the events section namesRestored<M>, and the "Which writes run hooks and events" table coversrestoreandforce_delete. The#[model]options table documentssoft_deletes.Tests
tests/model_soft_deletes.rsadds 8 SQLite integration tests covering:find, counts, andupdated_atwith_trashedandonly_trashedrestore, including arestoringhook that rejects the restoreforce_deleteon models and on queriesdelete,restore, andforce_deletehas_many/belongs_toqueries,with,with_count,has, and awith_countconstrained withwith_trasheddeleted_atThe full
databasesuite passes locally, exceptschema_dump_writes_the_sqlite_schema_with_the_migrations_rows, which also fails on0.xon my machine (macOS systemsqlite3) and is unrelated to this change.Not included
These felt better as follow-ups, to keep this PR reviewable:
assert_soft_deleted/assert_not_soft_deletedtesting assertions (Add the testing assertions Laravel developers expect #266 mentions these as waiting on soft deletes)with_trashedon route bindingsforce_deletingflag onDeletedor a separateForceDeletedeventprunable-style integration