Reputation: 3004
I've read the documentation on how to paginate results from within your database but I'm still very confused on where to put the 'pagination' code within my application. I want to go with the 'Paginating an Eloquent Model' approach since those are the types of models I'm using. I'm not sure where to put the 'paginate()' methods.
Should I build a custom controller that:
EDIT:
paginate_albums_Controller.php (Controller)
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 (View):
<div class="container">
<?php foreach($albums as $album); ?>
<?php echo $album->album_name; ?>
<?php endforeach; ?>
</div>
<?php echo $albums->links(); ?>
EDIT: If I use the structure above, could I use that custom controller and use it to call the 'index()' method from the third-party controller GalleryController.php that index.blade.php uses?
**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???? */
}
}
Any help is greatly appreciated since I'm still new to using Laravel PHP.
Upvotes: 0
Views: 412
Reputation: 3004
I figured out how to get pagination working:
Controller:
/* 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));
}
Edited the view in '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>
Pagination works fine now! Hope this gives a simple explanation on how to use simple pagination in Laravel 4 PHP.
Upvotes: 0
Reputation: 2428
I think that adding paginate to your index method in class GalleryController will fix this.
public function index()
{
/* Change: $allAlbums = $this->album->paginate(15); */
/* To: */
$allAlbums = Album::paginate(15);
$this->layout->content = \View::make('gallery::index', array('allAlbums' => $allAlbums));
}
Upvotes: 1
Reputation: 649
Though this may not be advocated, you can still check out ->all() function of album. Modify the code in the third party and add paginate() there.
paginate() function works on query result. If album->all() modifies the query result in one way or another, paginate() function may not work for it. If you are not comfortable in altering third party code, you may need to write your own code to paginate.
Upvotes: 2