Exemplo n.º 1
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index(Request $request)
 {
     $this->validate($request, ['search' => 'string|max:20', 'country_id' => 'integer|exists:country,id']);
     if ($request->has('search') && $request->has('country_id')) {
         $search = $request->input('search');
         $cities = City::whereHas('translations', function ($query) use($search) {
             $query->where('name', 'like', $search . '%');
         })->where('country_id', $request->input('country_id'))->paginate(15);
     } elseif ($request->has('search')) {
         $search = $request->input('search');
         $cities = City::whereHas('translations', function ($query) use($search) {
             $query->where('name', 'like', $search . '%');
         })->paginate(15);
     } elseif ($request->has('country_id')) {
         $cities = City::where('country_id', $request->input('country_id'))->paginate(15);
     } else {
         $cities = City::paginate(15);
     }
     if ($request->wantsJSON()) {
         return response()->json($cities->toArray(), 200);
     } else {
         return view('pages.city.index', ['cities' => $cities]);
     }
 }