-
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
[5.4] Custom Intermediate (Pivot) Models not instantiable #17770
Comments
Please post your entire custom class code. |
Hello @GrahamCampbell, Then, created a Test model: Modified the Test to extends from pivot:
Then on the web.php routes issued this:
Then going to my browser obtained this:
|
Reason for wanting to instantiate a custom pivot model on its own? My understand is it's for use within a relationship with the |
If you check the definition of Illuminate\Database\Eloquent\Relations\Pivot it extends from eloquent Model, so in my understanding it still being a regular model. |
I see your logic, it may extend Model but doesn't mean it completely reflects the same behaviour. Looks like the Pivot requires a parent model, and this is provided when the relationship builds that pivot here: I might be wrong so i would be interested too. I think the idea is so you can define some further logic on your custom pivot model. Eg when updating a property your might want to use a mutator or maybe define some helper methods on that pivot model which you couldn't do before. |
Yeah, it is possible that it does not need to reflects the same behaviour but so it should not extends directly from model but from a modified model or similar. |
Is there any progress? Getting this issue as well. |
@dees040 Unfortunately not. It seems like a not important issue yet. |
I have the same issue and error. The docs aren't really clear about the purpose of the $relation->using('model') functionality. For example is it possible to do something like this with that functionality: users
posts
post_user
post_user_role
$user->load('posts.pivot.roles'); That would be awesome, but can't get it to function. |
@RSDrsd you need to load in
|
I think the document should show a practical example using intermediate table. |
You are probably right. I would like to add an example to the documentation, but it seems like it does not work like I think it does, so may is a matter of time. |
Guys, agreed, some documentation would be nice, but after toying with it, here's how it works. Let's say M-M for User and Books. And you're also adding a
So now when you hit
Bingo. Now.... So now you might use it for something like this...
So that's it. Each row in the pivot table is now coming back to you as a BookUser model. And you can do relations, saves, updates, whatever. I hope that helps. |
Yes, it is the expected behavior here. |
Yes, BookUser can't be instanced directly. [edit...] It extends Pivot class which extends Model but for whatever reason is not quite the behavior as a normal Model. It's 'intermediary' and we just need to use it that way. It's very useful. |
@cjaoude Yes, it is a Pivot class, wich extends from Model. I clearly understand that Pivot is like a "special model," but it is extending from Model and should provide the same behavior expected from a regular model. In another case, it should extend from another kind of "Model." That is, in fact, the main reason for the discussion here. |
I understand what you mean. My posts are to answer how to use intermediary models, which some people have asked about above. |
Yes, you are right. Surely that will be very helpful for all. 💯 :) |
Now how can we eagerload the intermediate table?
|
I spend at least one week trying to find that question @rdpascua. |
I've created a concerete class instead of using the pivot :/
…Sent from my iPhone
On 27 Apr 2017, at 1:56 PM, DiakosavvasN <[email protected]<mailto:[email protected]>> wrote:
I spend at least one week trying to find that question @rdpascua<https://github.com/rdpascua>.
Anyways, after a long efford I tried a threeway pivot and works just fine. I don't know if it helps you.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#17770 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ACYexmwQCiEz90tyu4ZwAsiENI0RUgV9ks5r0C4YgaJpZM4L3VWH>.
|
@Dreambox13 @rdpascua can you provide an example of how you solved the problem of eager loading the intermediate table? |
i haven't, I just created a new model name based on the relation
…Sent from my iPhone
On 11 Jun 2017, at 6:35 AM, Danny Herran <[email protected]<mailto:[email protected]>> wrote:
@Dreambox13<https://github.com/dreambox13> @rdpascua<https://github.com/rdpascua> can you provide an example of how you solved the problem of eager loading the intermediate table?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#17770 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ACYexvhBV1vaNTZPuaDF2RA3VvFy2zMZks5sCxpJgaJpZM4L3VWH>.
|
I face similar issue but it's expected behavior because Pivot constructor needs some values indeed. Would be nice if we can use it as normal Model. |
Faced same issue recently. |
This proposal solved it for me @decadence @a-komarev : Read the comments all the way through. It explains the changes required to eager load a pivot model relationships on 5.4. |
@dherran Thanks for pointing to the solution. |
@dherran thanks. But would be cool to have this out of box. |
i'm having the same issue, i can't instantiate a pivot model, here's my structure and the use case posts (belongsToMany tags)
tags (belongsToMany posts)
post_tag (pivot)
votable (polymorphic)
and as the tables explain by themselves, you can vote for a post_tag just like the likedin tag recommandation feature the api would be something like : POST /post-tag/{post_tag}/likes
But this doesn't work because you can't instantiate, the only thing i can do now is POST /post/{post}/tag/{tag}/likes
that's so much cumbersome and so less performant as you have so many queries here while the only info needed is the pivot table record to save the associated vote, and not the tag record nor the post record |
Take a look at https://stackoverflow.com/a/44932979/2723301 I've implemented in a better way. |
@brunocascio the solution in your link doesn't make the pivot model instantiable since the pivots are gotten from the related query What we want here is |
Merged in 5.5. |
Awesome! Thank you @BertvanHoekelen |
@JuanDMeGon +1 for posting. I've gone hunting for this a couple of times; glad to see it make its way into the 5.5 release. Can anyone confirm if this addition to 5.5 will also allow for eager loading? eg, @rdpascua's example: |
@cjaoude How can I return data relate with pivot table to json without loop? |
I don't clearly understand why this issue gets to long in comments. I mean AFAIK following the Liskov substitution principle the behavior this had was is not correct. Now I'm looking a way to go workaround with this in 5.4. I have a |
Description:
When trying to use a custom intermediate model (a model that extends from
Illuminate\Database\Eloquent\Relations\Pivot
) as a regular model (Pivot
extends from Model), it fails, because is not able to create an instance.Too few arguments to function Illuminate\Database\Eloquent\Relations\Pivot::__construct(), 0 passed
I checked the commit but did not found anything would help on this: 4439576
Steps To Reproduce:
Create a model that extends from Pivot instead of Model directly:
Then try to use the model for some specific functions like all(), with(), query(), etc.
The text was updated successfully, but these errors were encountered: