2
votes

at the moment Iam really struggeling with filtering and the pagination of Laravel. On http://snkrgllry.herokuapp.com/ you can try to sort the images with the Filter Modal for example likes ascending, if you go now to the second page it says that view[2] is not found.

I send the parameters for filtering through a form to a controller which makes a query and will send all those data back to the view. Everything fine so far, but the second page is not working at all.

Here is my code on the Blade

<div class="container">
    <div class="col-xs-12 row-centered">
        {{ $images->appends(request()->input())->links()}}
        </div>
</div>

This is the controller function, please excuse my bad programming its my first web-/laravel project.

    public function filter(Request $request)


{
    $brand = $request->brand;
    $color = $request->color;
    $style = $request->style;
    $material = $request->material;
    $year = $request->year;
    $shape = $request->shape;
    $sorting = $request->sort;
    $page = $request->page;
    $user_id = $request->user_id;

    $sortingMethod = 'desc';
    $sortingParameter = 'created_At';

    //Abfrage wie sortiert werden soll

    if ($sorting == 'uploadDesc') {
        $sortingMethod = 'desc';
        $sortingParameter = 'created_At';
    } else if ($sorting == 'uploadAsc') {
        $sortingMethod = 'asc';
        $sortingParameter = 'created_At';
    } else if ($sorting == 'leer') {
        $sortingMethod = 'desc';
        $sortingParameter = 'created_At';
    } else if ($sorting == 'likesAsc') {
        $sortingParameter = 'count';
        $sortingMethod = 'asc';
    } else if ($sorting == 'likesDesc') {
        $sortingParameter = 'count';
        $sortingMethod = 'desc';
    }

    //$imagesQuery = DB::table('images')->select('brand', 'color', 'style', 'material', 'shape', 'year', 'id', 'path', 'created_at')->where('contest', 'true');
    $imagesQuery = DB::table('images')
        ->leftJoin('likes', 'images.id', '=', 'likes.image_id')
        ->select('images.*', DB::raw("count(likes.image_id) as count"))
        ->groupBy('images.id', 'images.brand', 'images.user_id', 'images.color', 'images.style', 'images.material', 'images.shape', 'images.year', 'images.desc', 'images.path', 'images.name', 'images.model', 'images.contest', 'images.remember_token', 'images.created_at', 'images.updated_at')
        ->orderBy($sortingParameter, $sortingMethod);


    $brands = DB::table('images')->select('brand')->groupBy('brand')->get();
    $colors = DB::table('images')->select('color')->groupBy('color')->get();
    $styles = DB::table('images')->select('style')->groupBy('style')->get();
    $materials = DB::table('images')->select('material')->groupBy('material')->get();
    $years = DB::table('images')->select('year')->groupBy('year')->get();
    $shapes = DB::table('images')->select('shape')->groupBy('shape')->get();


    if ($brand !== 'leer') {
        $imagesQuery->where('brand', '=', $brand);
    }

    if ($year !== 'leer') {
        $imagesQuery->where('year', '=', $year);
    }

    if ($color !== 'leer') {
        $imagesQuery->where('color', '=', $color);
    }

    if ($style !== 'leer') {
        $imagesQuery->where('style', '=', $style);
    }

    if ($material !== 'leer') {
        $imagesQuery->where('material', '=', $material);
    }

    if ($shape !== 'leer') {
        $imagesQuery->where('shape', '=', $shape);
    }

    if ($year !== 'leer') {
        $imagesQuery->where('year', '=', $year);
    }


    if ($page == 'contest') {
        $imagesQuery->where('images.contest', '=', 'true');

        $brands->where('contest', 'true');
        $colors->where('contest', 'true');
        $styles->where('contest', 'true');
        $materials->where('contest', 'true');
        $years->where('contest', 'true');
        $shapes->where('contest', 'true');
    }

    if ($page == 'profile') {
        $imagesQuery->where('images.user_id', '=', $user_id);
        $user = User::find($user_id);
    }

    $images = $imagesQuery->paginate(12);

    return view($page)->with(compact('images', 'brands', 'colors', 'styles', 'materials', 'years', 'shapes', 'user'));

}

And this is my route which will called, if I submit the filter form.

Route::get('/indexFilter', 'ImagesController@filter');

Are there any suggestions from your side how to fix this problem. I read a lot about it but I still didnt get it done.

I would really appreciate you help!

Best regards Lars

3

3 Answers

1
votes

You pass page as a parameter:

return view($page)

So it is '2' for the second page. Just pass template name, not page number. Use another name for passed page variable, it is used for pagination.

0
votes

Nice shoes! :)

The problem is in your frontend code. When you click the next page, the request param of page is 2 instead of index

Here's the HTML code: <a href="http://snkrgllry.herokuapp.com/indexFilter?_token=m2L6LXgKELcc66JsDgxFQaF6WxhfXfep22LC0PXk&amp;brand=leer&amp;color=leer&amp;style=leer&amp;material=leer&amp;shape=leer&amp;year=leer&amp;sort=likesAsc&amp;page=2">2</a>

0
votes

Thanks a lot I will not managed it to solve it until tomorrow (contribution of the project to our Prof. at University) but now I know my mistake and I "fixed" it so it will work out for me. I'll try my best to fix the issue with your solution!!