duan0821 2017-01-18 20:08
浏览 64
已采纳

通过表单(GET)发送参数后的Laravel分页

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条回答 默认 最新

  • douweibeng5219 2017-01-18 20:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?