Okipa/laravel-table

Eager load relations

RSickenberg opened this issue · 2 comments

Hello.

I'm trying to Eager load on the ->query() method but:

  1. I cannot use the Builder to set Eager loading
  2. I would like to make $myModel::with('myRelation') and use it on the table afterward.

Do you know how can I manage this?

Regards

Okay I found the solution like this. In case other people need this ;-)

$builder->with('getRelation')->where('col', '=', 'value');
And then:
$table->column()->title('title')->html(function (Model $model) { return $model->getRelationValue('relation')->myProp; });

Okipa commented

Hi @RSickenberg, sorry for the late answer !

That's indeed the way to lazy load a relationship, thanks for answering to other people with a potential similar issue.