Php

I can't find a native way to order by a relationship column paginated.

I am doing a clasic dashboard.. where data is displayed in a table paginated and sorted, usually the data are relationships of one main model... my question: I can't find a native way to order by a relationship column paginated.., still no way to do it, right? I have to do all maually with joins ?
$popularCurrencies = $this->presetPopularcurrencyModel->with([
'country', 'currency', 'locale'
])
->sortBy('country.code') // Imposible ??
->paginate(15);

I have to do it like..
$presetPopularCurrencyTable = PresetLocalepopularcurrencyModel::getTableName();
$currencyTable = CurrencyModel::getTableName();
$countryTable = CountryModel::getTableName();
$localeTable = LocaleModel::getTableName();

$popularCurrencies = $this->presetPopularcurrencyModel->select(.. all the columns ..)
->join($currencyTable, $presetPopularCurrencyTable.'.currency_id', '=', $currencyTable.'.id')
->join($countryTable, $presetPopularCurrencyTable.'.country_id', '=', $countryTable.'.id')
->join($localeTable, $presetPopularCurrencyTable.'.locale_id', '=', $localeTable.'.id')
->orderBy('currencies.code')
->paginate(15);

 
TLDR; there is not a native solution to sort by a relationship column with pagination , right?
that is a so common thing.. that I am amused that with all the fancy new things in Laravel it is not yet resolved.. so asking if maybe I am missing something
BTW https://github.com/fico7489/laravel-eloquent-join doesn't help on that case, because it can be used together with with() (eager loading)
You already invited:

Guilbaud

Upvotes from:

The package laravel-eloquent-join works, you were missng to put the trait in all the envolved models
 
$popularCurrencies = $this->presetPopularcurrencyModel->with([
'country', 'currency', 'locale'
])
->orderByJoin('locale.language')
->paginate(15);

If you wanna answer this question please Login or Register