英文:
Laravel Eloquent, apply select on my queries in whereHas
问题
以下是您的代码部分的中文翻译:
$prestations = Prestation::with('service', 'facility', 'conciergeries.network')
->whereHas('service', function ($query) use ($searchService) {
$query->addSelect('id', 'name')->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->addSelect('id', 'name')->where('name', 'regexp', "/$searchPartenaire/i");
})
->whereHas('conciergeries.network', function ($query) use ($searchFiliale) {
$query->addSelect('id', 'name')->where('name', 'regexp', "/$searchFiliale/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->paginate(50);
请注意,我已经将代码部分翻译成中文,只包括代码内容,没有其他信息。如果您需要任何其他帮助,请告诉我。
英文:
Here my code :
$prestations = Prestation::with('service','facility','conciergeries.network')
->whereHas('service', function ($query) use ($searchService) {
$query->addSelect('id','name')->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->addSelect('id','name')->where('name', 'regexp', "/$searchPartenaire/i");
})
->whereHas('conciergeries.network', function ($query) use ($searchFiliale) {
$query->addSelect('id','name')->where('name', 'regexp', "/$searchFiliale/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->paginate(50);
I want get only names and ids of "service", "facility" and "conciegeries.network".
I tried to use select('id','name');
and select(['id','name']);
or addSelect('id','name');
and pluck('id', 'name');
but i get all datas.
Do you have alternatives solutions ?
Thank you !
答案1
得分: 2
Select需要添加到with
中,而不是whereHas
。尝试使用以下代码:
Prestation::with([
'service' => function($query) {
$query->select(['id', 'name']);
},
'facility' => function($query) {
$query->select(['id', 'name']);
},
'conciergeries.network' => function($query) {
$query->select(['id', 'name']);
}
])
英文:
Select needs to be added to with
, not whereHas
. Try this instead:
Prestation::with(['service' => function($query) { $query->select(['id','name']);},
'facility' => function($query) { $query->select(['id','name']);},
'conciergeries.network' => function($query) { $query->select(['id','name']);}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论