laravel中的分页和工厂4

时间:2015-06-14 15:04:10

标签: php laravel-4

请考虑从here获取此代码。

 public function getIndex() 
 {
     $posts = Post::orderBy('id','desc')->paginate(10);
     // For Laravel 4.2 use getFactory() instead of getEnvironment() method.
     $posts->getEnvironment()->setViewName('pagination::simple');
     $this->layout->title = 'Home Page | Laravel 4 Blog';
     $this->layout->main = View::make('home')->nest('content','index',compact('posts'));
 }

据我所知,分页限制了行数,所以我认为paginate(10)意味着选择数据库中的前十行。但我绝对不明白这一点。

     // For Laravel 4.2 use getFactory() instead of getEnvironment() method.
     $posts->getEnvironment()->setViewName('pagination::simple');

     $posts->getFactory()->setViewName('pagination::simple');

以下所有内容。主要是我不明白工厂的意义以及它与分页的关系。我去了Illuminate\Pagination\FactoryIlluminate\View\View的laravel文档,但我找不到工厂的含义。任何人都可以解释上面的代码吗?

1 个答案:

答案 0 :(得分:1)

您实际上是通过选择特定的分页器视图来设置分页在HTML中的输出方式,这样您就可以在应用程序中使用多个类型,或者使用与默认值不同的类型。

  

在同一个应用程序中使用多个分页类型

     

有时,您可能希望在您的网页中使用不同的分页类型   应用。默认情况下,Laravel将使用您指定的类型   app / config / view.php文件,因此您需要覆盖此设置   你想使用另一种类型。这是如何做到的。

// This code should be in a controller or a route Closure.
// Let’s use the good old example of a list of blog posts.

$articles = Article::paginate(5);

Paginator::setViewName('pagination::simple');

/*
Alternatively, you could also use this to achieve the same result:

$articles->getEnvironment()->setViewName('pagination::simple');

For those who would like to know what’s happening under the hood, here is a more
detailed explanation:

1. Calling paginate() on an Eloquent model or a query builder will return an
   instance of \Illuminate\Pagination\Paginator

2. Then, we need to get the related \Illuminate\Pagination\Environment of this
   paginator via the well-named getEnvironment() method.

3. Finally, we can specify the pagination type we need. The default value is
   'pagination::slider'.

The pagination types that are available by default are located in the
vendor/laravel/framework/src/Illuminate/Pagination/views directory.
*/

来源:http://laravel-tricks.com/tricks/using-multiple-pagination-types-in-the-same-application