-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[5.4] Added ability to define custom class to use for pivot models. #14293
Conversation
Do we have any way to get tests for this? |
I can work on writing some tests for it. I did already confirm that all tests written at the time of the request passed. |
This would be a welcome addition for me 👍 |
I was hoping to get this feature in 5.3. Thanks for your work man |
I do use a lot of custom pivots... this will make my life easier ~ |
I work with some legacy databases with needs casting on model level. This would be really useful +1 |
|
||
if ($this->using) { | ||
$customPivotClass = $this->using; | ||
$columns = $customPivotClass::getPivotColumns(); |
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.
Why you have this new way of specifying which columns to load? Why not just use the traditional belongsToMany->with()?
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.
The reasoning behind this is that using the traditional method, you would need to defing the columns in every model using the custom pivot. So if you are doing a 3-way pivot, you would have to define the columns in up to 3 different models. That's bad duplication in my mind. The new method allows you to define the columns on the pivot model so you only have to define the columns in one sensible location.
Is there any interest in continuing this PR? |
I do want to continue it. I just don't have the time to work on the tests at the moment. |
Does this functionality differ from using "normal" models? Will they be called "pivot models" and work differently from existing models? |
I've added a test. Tests are still new to me, so any direction on additional tests are welcome. |
Please rebase this on our master. :) |
@GrahamCampbell Changes have been rebased. |
I merged this but removed some things to simplify it for now so we can just get the basic spirit of it in. I wanted to get it in the framework at least as a simple starting point. I removed the automatic eager loading from the constructor because I don't generally think database queries should be done just to new up an object. It makes it hard to new up something for testing, etc. I also removed the ability to define the attributes to load on the model itself mainly because I feel that is fairly easily recreated by just putting a public static property on the custom pivot model and doing: Thanks! |
By the way...
...you can't use the
Why are not all available by default as normal models? |
Moved PR over from 5.2 branch: #13304