【问题标题】:Laravel: How to specify the number of items in pagination via route variableLaravel:如何通过路由变量指定分页中的项目数
【发布时间】:2018-07-22 19:39:27
【问题描述】:

我希望更改通过分页显示的项目数量,通过以下路由在控制器中指定,以设置在每个分页页面上显示的产品数量。

例如,如果用户输入http://example.com/first-category?numProducts=99,他们将看到'pages.category' 视图,分页的每一页中有99 个项目。我想我的路线有问题,但需要一点帮助。

路线:

//First returns single product
Route::get('/products/{cat}/{permalink}', 'ShopController@show')->name('shop.product');
//Second gets all items in category group
Route::get('/products/{cat}', 'ShopController@subIndex')->name('shop.cat');
//Third to pass variable prodNum to ShopController for paginate($prodNum)
Route::get('/products/{cat}?numProducts={prodNum}', 'ShopController@subIndexMore')

ShopController.php

控制器如下所示:

// Shows all items in category, set to 10 items per page via paginate(10)
public function subIndex($cat) {
  $category = Category::where('permalink', $cat)->first();
  $products = ProductGroup::where('category', $category->id)->orderBy('id', 'asc')->paginate(10);
  return view('pages.category',compact('products', 'category'));
}
// Shows all items in category, paginate($prodNum) determines how many items in each page
public function subIndexMore($cat, $prodNum) {
  $category = Category::where('permalink', $cat)->first();
  $products = ProductGroup::where('category', $category->id)->orderBy('id', 'asc')->paginate($prodNum);
  return view('pages.category',compact('products', 'category'));
}

【问题讨论】:

    标签: php laravel-5 pagination


    【解决方案1】:

    您不需要在路由文件中指定查询字符串参数。您可以访问它们作为您的请求的输入。

    public function subIndex(Request $request, $cat)
    {
      $num_items = $request->input('numProducts');
      $category = Category::where('permalink', $cat)->first();
      $products = ProductGroup::where('category', $category->id)->orderBy('id', 'asc')->paginate($num_items);
      return view('pages.category',compact('products', 'category'));
    }
    

    记得包括:

    use Illuminate\Http\Request;
    

    【讨论】:

    • 这很好,正是我需要的,谢谢。从来没想过只指定请求
    • 对于以后发现此问题的其他人来说只是一个简短的说明,我将请求传递到视图中,并在我的刀片中使用以下代码更改第一个页面之后的任何页面的分页:@if($request->get('numProducts')) {{ $products->appends(['numProducts' => $request->get('numProducts')])->links() }} @else {{ $products->links() }} @endif
    【解决方案2】:

    例如http://example.com/first-category?numProducts=99,在这种情况下numProducts是控制器中使用的查询字符串参数$request

    public function subIndex(Request $request, $cat) 
    {
      $request->has('numProducts') ? $perPage = $request->get('numProducts') : 10;
    
      $category = Category::where('permalink', $cat)->first();
      $products = ProductGroup::where('category', $category->id)->orderBy('id', 'asc')->paginate($perPage);
      return view('pages.category',compact('products', 'category'));
    }
    

    如果你想在url中使用numProducts作为参数,设置路由如下:

    Route::get('/products/{cat}/{perPage}', 'ShopController@subIndexMore');
    

    控制者:

    public function subIndexMore($cat, $perPage) {
      $category = Category::where('permalink', $cat)->first();
      $products = ProductGroup::where('category', $category->id)->orderBy('id', 'asc')->paginate($perPage);
      return view('pages.category',compact('products', 'category'));
    }
    

    网址将是:

    http://example.com/first-category/99
    

    【讨论】:

    • @dinchliff 恐怕是第一个进来的。但是,对于您概述并添加的 $perPage,我已经接受了您的 if 语句。谢谢指点!
    猜你喜欢
    • 2015-04-11
    • 1970-01-01
    • 2018-10-20
    • 2021-08-26
    • 2017-08-07
    • 2019-10-07
    • 2016-04-09
    • 2019-10-19
    • 2015-06-16
    相关资源
    最近更新 更多