I have 3 tables business, service , service_venues. I need services of business having service_name = $search and and get venues of selected services. I am using services and resources method.
When there is search_keyword the data is not filtered. But other conditions are working. How can I fix this?
In business model
public function businessServices()
{
return $this->hasMany(BusinessService::class);
}
in businessService model
public function businessServiceVenues()
{
return $this->hasMany(BusinessServiceVenue::class);
}
In service file
public function getServices($business){
$businessservices = $business->with(['businessServices'=> function ($q){
$q->where('business_services.service_name', 'like', '%'.request()->search_key.'%');
}
])->first();
return new servicesResource($businessservices);
}
In service resource
$services = $this->businessServices()
->with(['businessServiceStaffs','businessServiceVenues'])
->when(request()->filled('venue_ids'), function ($q) use($request) {
$q->whereHas('businessServiceVenues', function($q1) use($request) {
$q1->whereIn('venue_id',$request->venue_ids);
});
})
->when(request()->filled('staff_id'), function ($q) use($request) {
$q->whereHas('businessServiceStaffs', function($q1) use($request) {
$q1->where('staff_id','=',$request->staff_id);
});
})
->get();
This works for me
$businessServices = $business->businessServices()
->with(['businessServiceStaffs', 'businessServiceVenues'])
->when(request()->filled('search_key'), function ($q) {
$q->where('business_services.service_name', 'like', '%' . request('search_key') . '%');
})
->when(request()->filled('venue_ids'), function ($q) {
$q->whereHas('businessServiceVenues', function ($q) {
$q->whereIn('venue_id', request('venue_ids'));
});
})
->when(request()->filled('staff_id'), function ($q) {
$q->whereHas('businessServiceStaffs', function ($q) {
$q->where('staff_id', '=', request('staff_id'));
});
})
->get();