Ad
Laravel Eloquent, Sort WhereHas / Width Datas
Here my code :
list($orderBy, $orderDirection) = explode('.', request()->get('sort_by'));
$prestations = Prestation::with([
'service' => function($query) {
$query->select(['id','name']);
},
'facility' => function($query) {
$query->select(['id','name']);
},
'conciergeries.network' => function($query) {
$query->select(['id','name']);
}
])
->whereHas('service', function ($query) use ($searchService) {
$query->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->where('name', 'regexp', "/$searchPartenaire/i");
})
->whereHas('conciergeries.network', function ($query) use ($searchFiliale) {
$query->where('name', 'regexp', "/$searchFiliale/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->paginate(50);
I need to to an orderBy on service.name
or facility.name
or conciergeries.network.name
, $orderBy
and $orderDirection
are coming from the view :
$orderBy
can have this values : 'name', 'service', 'facility', 'filiale'.
$orderDirection
can have this values : 'asc' or 'desc'.
I tried to add orderBy in the with query or in whereHas, but nothing works correctly. Thank you very much.
Ad
Answer
just add
'service' => function($query) use($orderBy, $orderDirection) {
$query->select(['id','name']);
$query->orderBy($orderBy, $orderDirection)
},
In with statements
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad