public function search(Request $request, $category_id) { $term = $request->input('q'); $items = App\Item::whereHas('categories', function ($q) use($category_id) { $q->where('id', '=', $category_id); })->where('name', 'like', '%' . $term . '%')->orderBy('name')->get(); return response()->json($items); }