php - Pagination with Slim Framework and Laravel's Eloquent ORM -
i'm using slim framework router, twig template engine , eloquent orm handle database.
i created bootstrap these libraries.
<?php require_once 'vendor/autoload.php'; /** * laravel eloquent orm */ use illuminate\database\capsule\manager 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 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 want is: how can paginate results of:
$foods = capsule::table('foods')->get();
considering have "two" pages, "/" , "/page/1" example.
and in each page want in max 30 results.
starting illuminate 4 can someting 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 include illuminate\pagination package , make work
capsule::table('foods')->paginate(5)
however don't know how bootstrap pagination class outside laravel.
consider taking @ https://github.com/zofe/rapyd-framework uses slim, twig, eloquent, symfony forms.. custom widgets pagination. (i'm author)
Comments
Post a Comment