please delete me
please delete me

Reputation: 167

Pagination with Slim Framework and Laravel's Eloquent ORM

I'm using Slim Framework as router, Twig as Template Engine and Eloquent ORM to handle the database.

I created the bootstrap for these libraries.

<?php

require_once 'vendor/autoload.php';

/**
 * Laravel Eloquent ORM
 */
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

$database_capsule = new Capsule;

$database_capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'username',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

$database_capsule->setEventDispatcher(new Dispatcher(new Container));

$database_capsule->setAsGlobal();

$database_capsule->bootEloquent();

/**
 * Twig Template Engine
 */
Twig_Autoloader::register();

$twig_loader = new Twig_Loader_Filesystem('template');

$twig_engine = new Twig_Environment($twig_loader);

And the routes:

/**
 * Slim Framework
 */
$application = new \Slim\Slim();

$application->get('/', function () use ($twig_engine) {

    $foods = Capsule::table('foods')->get();

    $index = $twig_engine->loadTemplate('index.html');

    echo $index->render(array('foods' => $foods));

});

$application->get('/page/:number', function ($number) use ($twig_engine) {

    $foods = Capsule::table('foods')->get();

    $index = $twig_engine->loadTemplate('index.html');

    echo $index->render(array('foods' => $foods));

});

$application->run();

What I want is: How can I paginate the results of:

$foods = Capsule::table('foods')->get();

Considering that I have "two" pages, the "/" and the "/page/1" for example.

And in each page I want in max 30 results.

Upvotes: 4

Views: 8238

Answers (3)

Nady Shalaby
Nady Shalaby

Reputation: 644

7- Don't forget to modify currentPageResolver and currentPathResolver. You can achieve this by using a simple middleware like the following.

<?php

namespace Mismar\Api\Middlewares;

use Illuminate\Pagination\Paginator;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Psr7\Response;

class PaginatorMiddleware
{
    /**
     * Example middleware invokable class
     *
     * @param  ServerRequest  $request PSR-7 request
     * @param  RequestHandler $handler PSR-15 request handler
     *
     * @return Response
     */
    public function __invoke(Request $request, RequestHandler $handler): Response
    {
        Paginator::currentPathResolver(function () use ($request) {
            return $request->getUri()->getPath();
        });
        Paginator::currentPageResolver(function ($pageName = 'page') use ($request) {
            return $request->getQueryParams()[$pageName] ?? 1;
        });

        return $handler->handle($request);
    }
}

Upvotes: 0

user5271805
user5271805

Reputation:

  1. Open composer.json
  2. Add this line in require section:

    "illuminate/pagination":"~5.0"

  3. Open your Ubuntu 14.04 terminal and go to your project's directory. Run composer update command. It will download your pagination library.

  4. Add these both Lines in your model file

    use Illuminate\Pagination;

    use Illuminate\Pagination\Paginator;

  5. In your controller place this code you will get paginated data.

    $data = Student::paginate(5);

  6. In order to get paginated links in view file.

    $students->links();

Upvotes: 3

Felice Ostuni
Felice Ostuni

Reputation: 1080

starting from illuminate 4 you can do someting like this

$application->get('/page/:number', function ($number) use ($twig_engine) {

    $foods = Capsule::table('foods')->skip(30*$number)->take(30)->get();
    $index = $twig_engine->loadTemplate('index.html');

    echo $index->render(array('foods' => $foods));

});

a better solution is to include Illuminate\Pagination package and make it work with

Capsule::table('foods')->paginate(5) 

however I don't know how to bootstrap Pagination class outside laravel.

Consider taking a look at https://github.com/zofe/rapyd-framework It uses Slim, Twig, Eloquent, Symfony Forms.. but custom widgets for pagination. (I'm the author)

Upvotes: 5

Related Questions