コード例 #1
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index(Request $request)
 {
     //listing all farmer when no search is done
     $allfarmer = User::where('user_type_id', 3)->paginate(10);
     //$allfarmer = User::where('user_type_id', 3)->get(); /previous code
     //listing Farmerpoint for dropdown
     $farmerPointList = FarmerPoint::Lists('name', 'id');
     $cropList = Crop::Lists('name', 'id');
     //recieving data from search field
     $farmer_name = $request->input('name');
     $farmer_phone = $request->input('phone');
     $farmerInPoint = $request->input('id');
     $farmerWithCrop = $request->input('crop');
     if (!empty($farmer_name)) {
         $allfarmer = User::where('user_type_id', 3)->where('name', 'LIKE', '%' . $farmer_name . '%')->paginate(10);
     }
     if (!empty($request->phone)) {
         $allfarmer = User::where('user_type_id', 3)->where('phone', 'LIKE', '%' . $farmer_phone . '%')->paginate(10);
     }
     if (!empty($farmerInPoint)) {
         if (!empty($farmerWithCrop)) {
             $allfarmer = User::whereHas('farmerCrop', function ($query) use($farmerWithCrop) {
                 $query->where('crop_id', $farmerWithCrop);
             })->where('user_type_id', 3)->where('farmer_point_id', $farmerInPoint)->paginate(10);
         } else {
             $allfarmer = User::where('user_type_id', 3)->where('farmer_point_id', $farmerInPoint)->paginate(10);
         }
     }
     if (!empty($farmerWithCrop)) {
         $allfarmer = User::whereHas('farmerCrop', function ($query) use($farmerWithCrop) {
             $query->where('crop_id', $farmerWithCrop);
         })->where('user_type_id', 3)->paginate(10);
     }
     return view('farmer.FarmerList', ['allfarmer' => $allfarmer, 'farmerPointList' => $farmerPointList, 'cropList' => $cropList]);
 }