-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[12.x] Add Eloquent Relationship Attributes #56415
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
[12.x] Add Eloquent Relationship Attributes #56415
Conversation
This reminds me of Doctrine, which I don't know if it's good or bad 😅 this implementation assumes that the method is called like the relationship by default and use the |
It assumes the conventional naming for the relationship, like between $user->posts;
$post->user; |
Hey there @taylorotwell, sorry to bother by mentioning you. I just wanted to check if I should change anything on this solution. I saw it was marked as a draft, but IDK if I should update it somehow. Thanks in advance. |
@ludo237 But it looks like this is customizable via the |
Yes it is |
A follow-up on this. |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
@taylorotwell you don’t have to use it, but I will! |
Overview
Since Laravel has been embracing the usage of Attributes, this PR introduces 12 new attributes for defining relationships in the Eloquent Models.
Why
Right now, we use methods in Model classes to define relationships. But this can lead to a mix of methods like relationships, scopes, and mutators mixed inside the class. By using Attributes, we group all relationship definitions in one place in the class. This makes it easier to see all the relations for a model at once.
Examples
These attributes work just like methods. You pass the arguments in the same order. Here are some examples from the test cases in this PR.