Convert Eloquent relationship to query builder for table?

Is it possible to use Eloquent relationships in the getTableQuery() method of the table builder? I want to do this because I have all of the relationships I want to use built as relationships, so rewriting them as queries seems like duplication. I also realise that I could use query scopes - but this also feels like duplication, as I wouldn't need the scopes anywhere else. I understand the table builder needs a query builder though - so I wondered if there was a way to convert something like a HasMany to an Eloquent query builder so I can use it in this way.
1 Reply
miomech
miomech17mo ago
You should be able to use toQuery on a model: https://laravel.com/docs/10.x/eloquent-collections#method-toquery In your table you should be able to use
protected function getTableQuery(): Builder
{
return $myModel->aRelationship()->toQuery();
}
protected function getTableQuery(): Builder
{
return $myModel->aRelationship()->toQuery();
}
I had something like that working at some point. Can't remember what I ended up doing 100% though.
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Want results from more Discord servers?
Add your server