i am facing a problem where my function have a request from the frontend for sorting the data. So i need to add some condition to my query. I tried to do this way.
$hawker = Hawker_location::withTrashed();
if($request->district){
$hawker->where('district', $request->district);
}
if($request->type){
$hawker->where('type', $request->type);
}
$hawker->paginate($request->sizePerPage);
return response()->json($hawker, Response::HTTP_OK);
But it return no value for the query but no error. how i can fix this ?
CodePudding user response:
Hawker_location::query()->withTrashed()
->when($request->has('district'), function($query) { $query->where('district', $request->district)})
->when($request->has('type'), function($query) { $query->where('type', $request-> type)})
->paginate($request->sizePerPage ?? 10)
CodePudding user response:
Try to return the paginator itself
return $hawker->paginate($request->input('sizePerPage',50));