-
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
Pivot models $table variable not respected / overridden #51410
Comments
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
Could you maybe try the solution from this issue? #48277 |
Hey @driesvints I started work on a possible fix but unsure if it's the right way to go forward. |
Thanks @edwwaarrdd. To be very honest, often when Pivot models are stretched like this we recommend to just use a dedicated eloquent model. I'm not sure if we should do any major changes. |
@shortontech @edwwaarrdd This is a very fundamental issue that can't be solved (without dirty hacks):
I agree that the solution should be documented, as this topic comes up regularly. |
@staudenmeir Yes I agree with this. Started tinkering a little bit but it became clear pretty fast that it's not something easy to fix. So @shortontech if you want to fix your issue instead of declaring your belongs to many like this:
You can write it this way:
And it will work the same as I am going to close my PR Thanks for the quick responses @driesvints & @staudenmeir |
Yeah I think we should as well. Thanks @staudenmeir. Would appreciate a PR to the docs if possible! |
Added a small extra section for it in the docs. |
Laravel Version
11.7.0
PHP Version
8.3.6
Database Driver & Version
mysql-community-server 8.0.35-1ubuntu22.04
Description
I created a pivot model to connect two tables, and set the protected $table attribute to a different table name that exists in my database. When attaching records, the $table property was not respected, and the getTable function must be overridden.
If no table is passed to
Illuminate\Database\Eloquent\Concerns\HasRelationships::belongsToMany
it generates a new table name and (eventually) sets the table name of the model to that value once that value is passed to `Illuminate\Database\Eloquent\Relations\Concerns\AsPivot::fromAttributes.There is no check on the model to ensure that the table name is set, and while I might imagine there could be concerns that the one writing the relationship would like to override the table, resolving the table name should be done later so that there's the possibility that it may be left to the Pivot once instantiated, given that the same table name resolution behavior is present in
Illuminate\Database\Eloquent\Relations\BelongsToMany::resolveTableName
andIlluminate\Database\Eloquent\Relations\Concerns\AsPivot::getTable
That is to say, they both transform the model name to snake case in singular form.
There is an undocumented behavior, in that you can pass a Pivot instance to the $table parameter, which does not run into this issue, but the name of the variable gives no hint at this behavior (and is undocumented).
Steps To Reproduce
$this->belongsToMany($relation)->using(PivotClass::class)
private $table
on the Pivot classI created the two regular models and one pivot model to connect the two, and when attaching one to the other I get the error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'my_proj.card_category' doesn't exist (Connection: mysql, SQL: insert into `card_category` (`category_id`, `card_id`) values (3, ?))
When I use tinker to check the table name, everything appears normal, which means that the Pivot model's $table attribute is being modified by setTable()
App\Models\CardCategory:
App\Models\Card:
The text was updated successfully, but these errors were encountered: