-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Support delete one document with the query builder #2591
Conversation
@@ -703,7 +703,14 @@ public function delete($id = null) | |||
$wheres = $this->compileWheres(); | |||
$options = $this->inheritConnectionOptions(); | |||
|
|||
$result = $this->collection->deleteMany($wheres, $options); | |||
if ($this->limit) { | |||
if ($this->limit !== 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should limit === 0
be supported? Or would Eloquent expect that to be a NOP?
I suppose the notion of limit: 0
meaning "delete all" is an implementation detail of MongoDB's delete
command and not a general concept, so I'm fine with not supporting that.
I do wonder if we should have a separate exception message if 0
is specified that advises users to specify null
instead (or not call limit()
at all), as this message might confuse a user with more experience with MongoDB than Eloquent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right. I've also banned 0.
Related issue: #2597 |
Custom implementation of MassPrunable is required to prevent using the limit. #2591 added an exception when limited is used because MongoDB Delete operation doesn't support it. - MassPrunable::pruneAll() is called by the command model:prune. - Using the parent trait is required because it's used to detect prunable models. - Prunable feature was introducted in Laravel 8.x by laravel/framework#37889. Users have to be aware that MassPrunable can break relationships as it doesn't call model methods to remove links.
Fix #2502
MongoDB PHP Library supports deleting one or all documents only. So we cannot accept other limits than
null
(unlimited) or1
.