-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] Add pending attributes #53720
base: 11.x
Are you sure you want to change the base?
Conversation
@tontonsb what do you think the behavior should be for many to many relationships? |
Sorry @taylorotwell, I did not realize that the I think it would be natural to expect that a relationship like this public function metaTags(): BelongsToMany
{
return $this->tags()
->withAttributes('visible', true)
->withPivotValue('type', 'meta');
} will select and create a Tag with I'm not 100% sure whether the querying should auto-qualify the attributes' columns for wheres. A plain On the other hand it seems that wrapping |
@tontonsb I do think that Does this PR work with morphable relationships? |
It does, I expanded the ManyToMany test with checks for
Done. @taylorotwell thanks for the guidance so far and let me know if anything else needs polishing here. |
Does User::query()->withAttributes('active', true)->create(); I don't think it makes sense. I feel like the name withAttributes doesn't really fit what it does. It feels like I'm going to add attributes to my model at the end, but behind the scenes it's adding a where condition! |
Yes @MahdiSaremi it does work with Eloquent's base builder. I can agree that in your example this feature is not very useful, but to me it would be useful in scopes as it allows creating a scope for both creation and selection. E.g. scopes that function in both
I'm not saying this is the best name ever, but it makes sense to me if I think of Part of the reason to add the where conditions is that |
Provides a
withAttributes()
method for Eloquent Builder. Doing->withAttributes(['key' => 'value'])
will instruct the Builder instance that the specified attributes must be added to new model instances if you create a model and used aswhere
conditions if you end up doing a select.Solves the same problem as #53677 (and satisfies its tests), but also provides
withAttributes
on the base builder.Details
Consider this setup:
Currently Laravel enables features like:
However, syntax like
$user->activePosts()->create()
orPost::active()->create()
will create a post without setting the value ofactive
. Which means that post can be created by$user->activePosts()->create()
but$user->activePosts()->get()
will not select it.This PR adds a
withAttributes
method that allows you to eitheror
And now the
$user->hiddenPosts()->create()
will create a Post with thehidden
attribute set to'y'
and the Post will be found by$user->hiddenPosts()->get()
.