This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I have several entities with their relationships configured in models.
The entities are:
- Item - has many option values
- Option Value - has one Option, belongs to many Items
- Option - has many option values
The problem is, when i'm trying to access related entites from Item entities, ORM performing multiple queries instead of a few what should be configured somehow in component. I've already managed to pre-load first-level relations using ::with method, but don't know how to pre-load second-level relations. e.g.: i've pre-loadend optionvalues and when i'm accessing item.optionvalue.option.name the ORM makes trip to database to get Option entity. What is the best way to solve this issue? Also, what is the best way to create proxy-accessor in OptionValue entity what will provide access to related Option's entity parameter, so i will able to write item.optionvalue.name instead of item.optionvalue.option.name?
Last updated
I resolved the main issue by myself:
To eager load nested relationships you should write them just like this:
$items = Item::available()->with([
'optionvalues.option' => function ($query) {
$query->orderBy('created_at', 'asc');
}
]);
Last updated
Other issue also has been resolved: (at OptionValue model)
public function getNameAttribute()
{
return $this->option->name;
}
1-4 of 4