Eager Loading of Relation data in OctoberCMS

187 views Asked by At

I am using a form that has a relation field inside. Although this is fine when few items are in the relation it gets quite slow when there are a lot. I wish to eager load some of the relationships to improve response times but I find it hard to find where I can access the query builder before it is run.

If I were using a standard list view I would go about doing this by extending the list query as below:

public function listExtendQuery($query)
{
    $query->with(['photos'])
}

But I have no idea how I can perform the exact same thing when loading the list using the relation widget. I have gone through the documentation multiple times but I did not manage to get this working.

Can anyone assist on this?

1

There are 1 answers

2
Eoler On

Would you believe if we told you that formExtendQuery method is a thing? :)

public function formExtendQuery($query)
{
    $query->with(['photos']);
}