Returning Relationships from `query()` method
Closed this issue · 1 comments
mikemand commented
Something I want to note for anyone else that may stumble upon this issue: If you return a relationship from the query()
method (example below) make sure you call ->getQuery()
at the very end to transform the return type from one of the relationships (abstract class: \Illuminate\Database\Eloquent\Relations\Relation
) to an Eloquent Builder. The exception thrown is as follows:
Symfony\Component\Debug\Exception\FatalThrowableError Argument 1 passed to Kdion4891\LaravelLivewireTables\TableComponent::attribute() must be an instance of Illuminate\Database\Eloquent\Builder, instance of Illuminate\Database\Eloquent\Relations\HasMany given
An example of a bad query:
public function query()
{
return $this->building->expenses()->where('type', 'monthly');
}
And now for a good:
public function query()
{
return $this->building->expenses()->where('type', 'monthly')->getQuery();
}
Edit: I should note this only causes a problem when searching or sorting. Pagination might be fine, I didn't test that.
kdion4891 commented
Interesting find. If it’s a relationship I may need to alter the logic.