Laravel Eloquent,排序 whereHas / 带数据宽度

huangapple go评论70阅读模式
英文:

Laravel Eloquent, sort whereHas / width datas

问题

这是我的代码:

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);

我需要在service.namefacility.nameconciergeries.network.name上进行排序,$orderBy$orderDirection来自视图:

$orderBy可以取以下值:'name'、'service'、'facility'、'filiale'。

$orderDirection可以取以下值:'asc'或'desc'。

我尝试在with查询或whereHas中添加orderBy,但没有正确工作。非常感谢。

英文:

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.

答案1

得分: 2

'服务' => function($query) use($orderBy, $orderDirection) {
$query->select(['id','name']);
$query->orderBy($orderBy, $orderDirection)
},

英文:

just add

'service' => function($query) use($orderBy, $orderDirection) { 
            $query->select(['id','name']);
            $query->orderBy($orderBy, $orderDirection)
        },

In with statements

huangapple
  • 本文由 发表于 2020年1月6日 21:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定