Ad
Laravel Eloquent, Apply Select On My Queries In WhereHas
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 !
Ad
Answer
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']);}
])
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