【问题标题】:Laravel Pagination after sending parameters through form (GET)通过表单(GET)发送参数后的Laravel分页
【发布时间】:2017-06-03 08:53:33
【问题描述】:

目前我真的在为过滤和 Laravel 的分页而苦苦挣扎。在http://snkrgllry.herokuapp.com/ 上,您可以尝试使用过滤器模式对图像进行排序,例如喜欢升序,如果您现在转到第二页,它会说未找到视图[2]。

我将用于过滤的参数通过表单发送到控制器,该控制器进行查询并将所有这些数据发送回视图。到目前为止一切都很好,但是第二页根本不起作用。

这是我在 Blade 上的代码

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

这是控制器功能,请原谅我的第一个 web-/laravel 项目编程不当。

    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'));

}

如果我提交过滤表单,这就是我的路由。

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

您对如何解决此问题有任何建议吗?我读了很多关于它的内容,但我仍然没有完成。

非常感谢您的帮助!

最好的问候拉尔斯

【问题讨论】:

    标签: php html laravel pagination


    【解决方案1】:

    您将页面作为参数传递:

    return view($page)
    

    所以第二页是“2”。只需传递模板名称,而不是页码。为传递的page变量使用另一个名称,它用于分页。

    【讨论】:

      【解决方案2】:

      鞋子不错! :)

      问题出在您的前端代码中。点击下一页时,page的请求参数是2而不是index

      这是 HTML 代码:&lt;a href="http://snkrgllry.herokuapp.com/indexFilter?_token=m2L6LXgKELcc66JsDgxFQaF6WxhfXfep22LC0PXk&amp;amp;brand=leer&amp;amp;color=leer&amp;amp;style=leer&amp;amp;material=leer&amp;amp;shape=leer&amp;amp;year=leer&amp;amp;sort=likesAsc&amp;amp;page=2"&gt;2&lt;/a&gt;

      【讨论】:

        【解决方案3】:

        非常感谢,直到明天我才能解决它(该项目对我们大学教授的贡献)但现在我知道我的错误并且我“修复”了它,所以它会为我解决。我会尽力用您的解决方案解决问题!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-07
          相关资源
          最近更新 更多