Skip to content
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

Idea: Possible to eager load pivot relations on a model instance? #5

Open
empresarrollo opened this issue May 11, 2020 · 4 comments
Open

Comments

@empresarrollo
Copy link

Is it possible to load pivot relations on a model instance?

Taking the example on the README, say you have a controller method:

public function index(Plan $plan)
{

}

It would be great to be able to do something like:
$plan->load('items.planItem.unit');
or better yet:
$items = $plan->items()->orderBy('name')->with('planItem.unit')->get();
to return that to the view

thanks!

@ajcastro
Copy link
Owner

@empresarrollo I believe that should work

@empresarrollo
Copy link
Author

@ajcastro
You are right. with $plan->load('items.planItem.unit'); I've made it work.

But I cannot eager load the pivot relation going from "items()":
When I do
$items = $plan->items()->orderBy('name')->with('planItem.unit')->get();
i got:
Call to undefined relationship [planItem] on model [App\Item].

I don't think it's possible.

@lidoma
Copy link

lidoma commented Jul 5, 2020

I have the same issue. Any updates?

@ajcastro
Copy link
Owner

ajcastro commented Oct 1, 2020

@empresarrollo @lidoma , sorry for late reply.
Here's how to make it work.
You need to define a hasOne planItem() relation in Item model. That way we, can eagerload the planItem relation model coming from Item model.

class Item extends Model
{
    public function planItem()
    {
        return $this->hasOne(PlanItem::class);
    }
}

Keep in mind that item has many planItem in actual, but since we only care for one planItem for each plan, then we define a hasOne planItem relation instead. We will just need to pass the plan_id in the eager load query to make sure we are getting the correct planItem for the plan and item:

$items = $plan->items()
->with([
    'planItem' => function ($query) { 
        $query->where('plan_id', $plan->id); 
    },
    'planItem.unit'
])
->get()


dd($items->toArray());
/*
[
    [
        'id' => '',
        'name' => '',
        'description' => '',
        'planItem' => [
            'plan_id' => '',
            'item_id' => '',
            'unit_id' => '',
            'qty' => '',
            'price' => '',
            'unit' => [
                'id', 
                'name' => '',
                'description' => '',
            ],
        ],
    ], [
        'id' => '',
        'name' => '',
        'description' => '',
        'planItem' => [
            'plan_id' => '',
            'item_id' => '',
            'unit_id' => '',
            'qty' => '',
            'price' => '',
            'unit' => [
                'id', 
                'name' => '',
                'description' => '',
            ],
        ],
    ],
]
 */

I haven't tested it yet but I believe it should work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants