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
Ad