Skip to content
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

Eloquent: increment() on Model filtered out by default global scope doesn't get persisted into the database #47620

Closed
levu42 opened this issue Jun 30, 2023 · 0 comments · Fixed by #47629

Comments

@levu42
Copy link
Contributor

levu42 commented Jun 30, 2023

Laravel Version

10.4.1

PHP Version

8.2.7

Database Driver & Version

MySQL & SQLite, both broken

Description

When a Model class has a default global scope, that filters out some records when retrieving them, but you still retrieve them through withoutGlobalScope(ScopeName::class), it's not always possible to use ->increment() on this model instance.

The change to the column that should be incremented is saved in the model's attributes, but isn't persisted into the database. Most likely from my debugging (without understanding Eloquent internals) it looks like the query to update the column in the database applies default global scopes, so it can't find the row to update in the database.

Attached is a repo with a test failing at exactly that point.

What I found out while debugging: This doesn't apply to all global scopes. When the global scope only applies a simple ->where('remaining_times', '>', 0), not the complex (where remaining_times IS NULL or remaining_times > 0), it seems to work fine. I don't know why that is.

Steps To Reproduce

See failing test in levu42/increment-global-scope-bug@7b514be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant