-
Notifications
You must be signed in to change notification settings - Fork 22
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
Comments
@empresarrollo I believe that should work |
@ajcastro But I cannot eager load the pivot relation going from "items()": I don't think it's possible. |
I have the same issue. Any updates? |
@empresarrollo @lidoma , sorry for late reply. class Item extends Model
{
public function planItem()
{
return $this->hasOne(PlanItem::class);
}
} Keep in mind that $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 |
Is it possible to load pivot relations on a model instance?
Taking the example on the README, say you have a controller method:
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!
The text was updated successfully, but these errors were encountered: