Laravel 4 PHP:使用分页来限制数据库结果


Laravel 4 PHP: Using pagination to limit database results

我已经阅读了关于如何在数据库中对结果进行分页的文档,但我仍然对在应用程序中将"分页"代码放在哪里感到困惑。我想采用"寻呼Eloquent模型"的方法,因为这些都是我正在使用的模型类型。我不确定将"paginate()"方法放在哪里。

我应该建立一个自定义控制器:

编辑:

  • 从"第三方"控制器(GalleryController.php)检索所有数据库结果
  • 将结果分页到我的自定义控制器中的一个函数中,例如:

paginate_albums_Controller.php(控制器)

use JeroenG'LaravelPhotoGallery'Controllers'GalleryController; // Path of third-party controller
use JeroenG'LaravelPhotoGallery'Controllers'AlbumsController;  // Path of third-party controller
    public function paginate_results($id)
    {                         
         $allAlbums = $this->album->paginate(15);
    }

index.blade.php(视图):

 <div class="container">
       <?php foreach($albums as $album); ?>
       <?php echo $album->album_name;  ?>
       <?php endforeach; ?>
 </div>
 <?php echo $albums->links(); ?>

编辑:如果我使用上面的结构,我可以使用该自定义控制器,并使用它从index.blade.php使用的第三方控制器GalleryController.php调用"index()"方法吗?

 **GalleryController.php (Controller that index.blade.php currently uses):**
 class GalleryController extends BaseController {
/*
 * The album model
 **/
protected $album;
/*
 * The photo model
 */
protected $photo;
/*
 * Instantiate the controller
*/
public function __construct()
{
    $this->album = 'App::make('Repositories'AlbumRepository');
    $this->photo = 'App::make('Repositories'PhotoRepository');
}
/*
 * Listing all albums
 */
public function index()
 {
    $allAlbums = $this->album->all();
    $this->layout->content = 'View::make('gallery::index', array('allAlbums' => $allAlbums));
   /* Is it possible to add code here that will access my custom controller 'paginate_albums_Controller' to paginate the number of albums???? */
 }

}

任何帮助都是非常感谢的,因为我仍然是使用Laravel PHP的新手。

尽管这可能不是提倡的,但您仍然可以查看相册的->all()函数。修改第三方中的代码,并在其中添加paginate()。

函数paginate()处理查询结果。如果album->all()以这样或那样的方式修改查询结果,paginate()函数可能不适用。如果您对修改第三方代码感到不舒服,您可能需要编写自己的代码来进行paginate。

我认为在类GalleryController中向索引方法添加分页会解决这个问题。

 public function index()
{
   /*  Change: $allAlbums = $this->album->paginate(15); */
   /*  To: */
   $allAlbums = Album::paginate(15);  
   $this->layout->content = 'View::make('gallery::index', array('allAlbums' => $allAlbums));
}

我发现了如何使分页工作:

  • 通过更改我的"routes.php"文件,获取第三方控制器并使其成为本地控制器
  • 编辑了控制器,使索引函数如下所示:

控制器:

/* Add the layout file to a folder named 'layouts' in your app/views path 
  This will prevent a 'Creating a default object from empty value reference' from occurring */
protected $layout = 'layouts.master';
public function index()
{
    $allAlbums = Album::paginate(10);
    $this->layout->content = 'View::make('gallery::index', array('allAlbums' => $allAlbums));
}

在"index.blade.php"中编辑视图:

<div class="panel-body">
    @if ($allAlbums->count())
    <dl class="dl-horizontal">
    @foreach($allAlbums as $album)
    /* Database column code goes here */
    @endforeach
    {{ $allAlbums-> links()}} // Added this code for the pagination links to actually appear
    </dl>
</div>

寻呼现在运行良好!希望这能简单解释一下如何在Laravel 4 PHP中使用简单的分页。