Jeroen-G / laravel-photo-gallery

A photo gallery for Laravel
40 stars 32 forks source link

Adding pagination to album results or '$allAlbums' variable #23

Closed jeffpaitchell closed 9 years ago

jeffpaitchell commented 9 years ago

I've been trying to add pagination to my albums results by using the 'paginate()' method within the 'GalleryController.php' controller but I'm having issues with properly editing and updating this controller so that pagination will take effect.

Steps I've taken so far:

1) Edited 'routes.php' and changed:

Route::get('gallery', array('as' => 'gallery', 'uses' => 'JeroenG\LaravelPhotoGallery\Controllers\GalleryController@index'));

to:

Route::get('gallery', array('as' => 'gallery', 'uses' => 'GalleryController@index'));

If I run my local version of 'GalleryController.php', I get a 'Creating default object from empty value' within the function:

public function index()
{
    $allAlbums = $this->album->all();
    $this->layout->content = \View::make('gallery::index', array('allAlbums' => $allAlbums));
}

After I eventually make the fix to this function, I would like to change '$allAlbums' so that it will paginate results to 10 results per page. So would the code below accomplish this task:

$allAlbums = Album::paginate(10);
Jeroen-G commented 9 years ago

Which of those lines gives the error? What does your controller look like? Regarding pagination, have a look at how the pagination of photos inside the album is handled. It's just a matter of copying that I suppose.

To help you, this is in the view:

<div class="panel-footer clearfix">
           <?php echo $albumPhotos->links(); ?>
</div>

And this is in the photo repository:

public function findByAlbumId($albumId)
    {
        return Photo::where('album_id', '=', $albumId)->paginate(10);
    }
jeffpaitchell commented 9 years ago

'GalleryController.php':

    public function index()
    {
        $allAlbums = $this->album->all();
        /* Error occurs on line of code below */
        $this->layout->content = \View::make('gallery::index', array('allAlbums' => $allAlbums));
    }

Regarding the line of code you mentioned above about paginating in the photo repository, all I want to do is paginate album results on main index page. I just want to limit the album results by 10 instead of just displaying all of the albums stacked on top of each other.
To accomplish this task, can I just paginate all the 'Album Results' or '$allAlbums' by 10?

Jeroen-G commented 9 years ago

No. paginate() is part of Laravel's ORM query and cannot be performed on returned results. I wanted to see how your controller looks. Just as with your other question you probably should bypass the repositories and use the Model to perform a query with pagination. I will add album pagination in a future update.

jeffpaitchell commented 9 years ago

Ok, I'm going to try updating the 'Albums' model and build queries so that my album results will be paginated. I'll keep you posted!

Jeroen-G commented 9 years ago

No need to touch the model file. http://laravel.com/docs/4.2/pagination

jeffpaitchell commented 9 years ago

I've actually read this documentation and I'm confused on where to actually put the 'pagination' lines of code:

$allUsers = User::paginate(15);
$someUsers = User::where('votes', '>', 100)->paginate(15); 

Do they go in my 'AlbumsController.php' file or do I just modify 'index.blade.php' to include these php statements? Sorry I'm still confused :(

Or should I build a custom controller that will paginate these results and then display it using the 'links()' method within my 'index.blade.php' file?

Jeroen-G commented 9 years ago

Query in the controller, links in the view. Look how I did it in this package. I abstracted the query to a repository but that's not necessary in your case.

jeffpaitchell commented 9 years ago

Ok I finally got it working after taking your advice!!! So here's what I did so that people in the future can add pagination to their album results:

1) Make 'GalleryController.php' a local controller by going into 'routes.php' and changing the route path to:

Route::get('gallery', array('as' => 'gallery', 'uses' => 'GalleryController@index'));

2) Edit 'GalleryController.php' so that it becomes:

    <?php

    use JeroenG\LaravelPhotoGallery\Controllers\AlbumsController;

    use JeroenG\LaravelPhotoGallery\Models\Album;

    use JeroenG\LaravelPhotoGallery\Validators as Validators;

    class GalleryController extends BaseController {

    /*
     * The album model
     */
    protected $album;

    /*
     * The photo model
     */
    protected $photo;

       /* Add this code below and put 'master.blade.php' into a 'layouts' folder within your local 'Views' directory */
    protected $layout = 'layouts.master';

    /*
     * 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 = Album::paginate(10);
        $this->layout->content = \View::make('gallery::index', array('allAlbums' => $allAlbums));
    }
    }

3) Make sure you have 'master.blade.php' in 'app\views\layouts'.

4) Run 'php artisan dump-autoload' so that your class changes take effect

5) Refresh your browser and you should see the pagination.

Thanks so much Jeroen for helping me out with this!