-
Notifications
You must be signed in to change notification settings - Fork 11k
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] Added for
method to Eloquent models
#53610
Conversation
Yes, please! 🤩🤩 |
I love it. The implementation looks simple and easy to maintain, whilst the feature is very useful. |
Wouldn't it be better if this was powered by actual relationship method assignments? So $store->owner()->assign($user); If I recall correctly |
I can see the benefit, but it feels a bit ambiguous and might raise questions if I came across it while browsing the code—or for juniors who might struggle to decide which approach to use. The existing methods are a bit more explicit in their intent. Cool idea though! |
You already can do |
@ericwoelki right, but we are talking about |
@osbre Fair, I was pointing out that the method is |
This change makes sense because building relations in this way is already implemented in Already valid code for building eloquent models in tests (and best practice how to do it): $post = Post::first();
$user = User::first();
$comment = Comment::factory()
->for($post)
->for($user)
->make(['comment' => 'hello']); |
It doesn't look like this will work for all relations specifically i am thinking of ManyToMany relations and Morphed relations. |
I think I will hold off on this one for now. A bit worried such a short, common word could already exist in applications and cause breaking changes. I also wonder if you could just use factories. |
Hey!
This PR proposes a new
for
method that helps set foreign keys on models. It's a fresh (and simplified!) take on my past attempt from a couple of years ago: #42467I think a major drawback of the previous approach was that it wasn't obvious whether
for
would be used in queries, or for setting fields on models. So it got a bit confusing!In this approach, I've completely ditched the Builder side of things. Although on the surface, it looks like it would have been nice, I think it would have caused more issues than it solved! So, instead, the
for
method only exists on the Model classes.So you can call it like so:
Assuming there is a
posts
andusers
relationship, the comment will now have the following data set:There's also the ability to specify the relationship name if needed:
I also quite like how it almost reads like a human-readable sentence when it's written like this too:
Hopefully this is something that other devs would also find useful. Although it's a relatively small change, I like the way that it can reduce the likelihood of typos and also uses a similar syntax to model factories.
But I completely understand if it's not something worth merging in. I'd kick myself if I didn't try and scratch the itch though haha!
If it's something you might consider merging, please let me know if there's anything you might want me to change 😄