This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

miller
miller

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

miller
miller

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

miller
miller

Other issue also has been resolved: (at OptionValue model)


    public function getNameAttribute()
    {
        return $this->option->name;
    }

daftspunky
daftspunky

Nice work!

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.