我已经阅读了有关如何在数据库中对结果进行分页的文档,但我仍然对将“分页”代码放在应用程序中的位置感到困惑。我想采用'Paginating an Eloquent Model'方法,因为那些是我正在使用的模型类型。我不确定在哪里放'paginate()'方法。
我应该构建一个自定义控制器:
编辑:
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(); ?>
编辑:如果我使用上面的结构,我可以使用该自定义控制器并使用它从第三方控制器 GalleryController.php 调用'strong()'方法 index.blade.php 使用?
**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。
答案 0 :(得分:2)
虽然这可能不被提倡,但你仍然可以查看 - > all()相册功能。修改第三方中的代码并在那里添加paginate()。
paginate()函数适用于查询结果。如果album-&gt; all()以某种方式修改查询结果,则paginate()函数可能无法正常工作。如果您不愿意更改第三方代码,则可能需要编写自己的代码进行分页。
答案 1 :(得分:1)
我认为在类GalleryController中为你的索引方法添加paginate会解决这个问题。
public function index()
{
/* Change: $allAlbums = $this->album->paginate(15); */
/* To: */
$allAlbums = Album::paginate(15);
$this->layout->content = \View::make('gallery::index', array('allAlbums' => $allAlbums));
}
答案 2 :(得分:0)
我想出了如何让分页工作:
<强>控制器:强>
/* 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&#39;中编辑了视图:
<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中使用简单的分页。