Пример #1
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function getIndex()
 {
     $per_page = Input::get('per_page', 100);
     $page = Input::get('page', 1);
     $level = Input::get('level', null);
     $skip = ($page - 1) * $per_page;
     // validate
     $rules['per_page'] = ['numeric', 'between:1,100'];
     $rules['page'] = ['numeric', 'min:1'];
     $rules['level'] = ['in:continent,country,province,city,suburb'];
     $validator = Validator::make(['per_page' => $per_page, 'page' => $page, 'level' => $level], $rules);
     if ($validator->fails()) {
         return response()->json(JSend::fail($validator->messages()->toArray()));
     }
     //
     $locations = Location::level($level)->orderBy('path')->skip($skip)->take($per_page)->get();
     if ($locations->count()) {
         return response()->json(JSend::success($locations->toArray())->encode());
     } else {
         return response()->json(JSend::fail(['Data not found'])->encode());
     }
 }