/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $formdata = $request->all(); // $categories = Category::lists('name', 'id'); // $days = Day::lists('dayname', 'id'); // ADD ANY/ALL OPTION $days = array(8 => 'Any') + Day::lists('dayname', 'id')->toArray(); $categories = array(4 => 'All') + Category::lists('name', 'id')->toArray(); $lat = $request->get('lat'); $lng = $request->get('lng'); $chosenDay = $request->get('dayList'); $deals = Deal::all()->lists('dealname', 'dayID')->toArray(); // GET DISTANCE FROM USER // $distance = $request->get('distance'); // USE PRE-DEFINED DISTANCE $distance = 100; // THIS GETS THE USER DEFINED CATEGORY CHOICES $categoryChoice = $request->get('categoryList'); // $dayChoice = $request->get('dayList'); // dd($categoryChoice); // IF USER MAKES NO CATEGORY CHOICE SHOW ALL if (count($categoryChoice) == 0) { // USER MUST CHOOSE SOMETHING // \Session::flash('flash_message', 'You Must Choose a Category'); // return redirect()->back(); // SHOW ALL IF NO CHOICE IS MADE $categoryChoice = Category::lists('id'); } elseif (in_array(4, $categoryChoice)) { $categoryChoice = Category::lists('id'); } else { $categoryChoice = $request->get('categoryList'); // dd($categoryChoice); } if (count($chosenDay) == 0) { // SHOW ALL IF NO CHOICE IS MADE $chosenDay = Day::lists('id'); } elseif (in_array(8, $chosenDay)) { $chosenDay = Day::lists('dayname', 'id'); $chosenDay = $chosenDay->toArray(); $chosenDay = array_keys($chosenDay); } else { $chosenDay = $request->get('dayList'); } $query = Article::distance($lat, $lng, $distance)->whereHas('categories', function ($query) use($categoryChoice) { $query->whereIn('categories.id', $categoryChoice); })->whereHas('days', function ($query) use($chosenDay) { $query->whereIn('days.id', $chosenDay); })->get(); $articles = $query; if (count($articles) == 0) { // Flash::message('Sorry no deals in your area.'); \Session::flash('flash_message', 'Sorry no deals in your area. Try Again!'); return redirect()->action('HomeController@index'); } return view('articles.index', compact('categories', 'articles', 'days', 'formdata', 'deals'))->with('startLat', $lat)->with('startLng', $lng)->with('chosenDay', $chosenDay); }