Пример #1
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index(Request $request)
 {
     $sortField = $request->get('sortField');
     $sortOrder = $request->get('sortOrder');
     $displayCount = $request->get('displayCount');
     $pageTitle = 'Recipe listing';
     $pageText = '';
     $viewAllLink = '';
     if ($sortField == 'search') {
         $sortLabel = 'search results';
         $pageTitle = 'Search Results';
     } else {
         // since we use the sort order param to grab the search params only check if this is NOT a search
         if ($sortOrder != 'desc') {
             $sortOrder = 'asc';
             $orderLabel = "Ascending";
         } else {
             $orderLabel = "Descending";
         }
         // if not a search validate the sort fields too
         if ($sortField != 'date_added') {
             $sortField = 'name';
             $sortLabel = "sorted by recipe name";
         } else {
             $sortLabel = "sorted by date added";
             $pageTitle = 'Recent recipes';
         }
     }
     if ($displayCount != 'all') {
         $displayCount = intval($displayCount, 10);
         if ($displayCount < 20) {
             $displayCount = 20;
         }
         $countLabel = '';
         if ($sortField == 'search') {
             $recipes = Recipe::search($sortOrder)->paginate($displayCount);
         } else {
             $recipes = Recipe::orderBy($sortField, $sortOrder)->paginate($displayCount);
         }
         if ($recipes->lastPage() > 1) {
             $pageText = " (page " . $recipes->currentPage() . ' of ' . $recipes->lastPage() . ')';
             $viewAllLink = $request->path() . '?sortOrder=' . $sortOrder . '&sortField=' . $sortField;
         }
     } else {
         if ($sortField == 'search') {
             $recipes = Recipe::search($sortOrder)->get();
         } else {
             $recipes = Recipe::orderBy($sortField, $sortOrder)->get();
         }
         $countLabel = 'All';
     }
     $titleDetail = $sortLabel . $pageText;
     //$recipes = Recipe::all();
     //return view('recipe.index', [$recipes, $displayCount, $sortLabel, $orderLabel, $sortField, $sortOrder, $displayCount]);
     //return view('recipe.index', [$recipes, $displayCount, $sortLabel, $orderLabel, $sortField, $sortOrder, $displayCount]);
     return view('recipe.index', compact('recipes', 'sortField', 'sortOrder', 'displayCount', 'titleDetail', 'pageTitle', 'viewAllLink'));
 }