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条)

报告相同问题?

悬赏问题

  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline