英文:
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.name
、facility.name
或conciergeries.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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论