-
-
Notifications
You must be signed in to change notification settings - Fork 40
Use pivot model table name if not defined in the relation #216
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
Closed
+56
−2
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efe85fc
Use pivot model table name if not defined in the relation
mjauvin 84ae111
add unit test for table defaulting to custom pivot model table
mjauvin ef09bca
add missing public keyword
mjauvin e4cbd68
pivotmodel first
mjauvin 99d72df
Demonstrate correct able name
bennothommo 1a35249
Marc was right, Ben was wrong
bennothommo 68f8f3d
also restore BelongsToMany relation table assignment order
mjauvin 3d5e806
add an extra test to make sure relation table has precedence
mjauvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Arguably, a pivot model definition should be considered more authoritative than a table definition, on the off-chance that someone specifies both in the config.
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.
There's nothing that ensures at this point that the pivot model defines the table...
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.
@mjauvin not sure what you mean? If you specify a pivot model, Laravel will determine the table of that model in the standard way (either the
$tableproperty of the model, or conventionally using the model's name).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.
Yes, but there's no way to know at that point if the pivot actually defines a table name.
If it doesn't, laravel's way of guessing the table name is not compatible with ours
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.
What's an example of this not working?
We already have a call to the
usingmethod underneath where you've made your edit, which should switch the relation to use the pivot's table.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.
@bennothommo I changed the order as you suggested, and added a [failing] test to demonstrate my point.
Uh oh!
There was an error while loading. Please reload this page.
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.
@mjauvin sorry, but I'm still not understanding how this is an issue?
Your failing test is creating a BTM relation called
pivot_without_tableand using theCustomPivotWithoutTableclass as a custom pivot without defining a table, and then expecting the table name to becustom_pivot_table, when by the normal automatic table name logic, it should becustom_pivot_without_tablewhich is correctly being returned by thegetTable()method.It should be said that when defining a BTM relation in Laravel, you define either the table OR the pivot, you do not define both. If your pivot needs to use a table name that doesn't match the convention, then you specify the table name in the pivot.EDIT: Never mind, it was buried deep in Laravel but evidently I'm wrong. The relation can still set the table name for a custom pivot.
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.
I've updated the test to show that it passes with the expected table name.