Eloquent paginate function in Slim 3 project using twig

How can I use paginate function from Eloquent in Slim 3 project using twig ?

This is in my controller :

$posts = Sound::paginate(2); $this->container->view->render($response, 'admin/sounds/index.twig', [ 'posts' => $posts ]);

This is the view :

{{ posts.links() }}

But it doesn't work as well as I expected :

Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in **PATH_TO_PROJECT**\vendor\illuminate\pagination\AbstractPaginator.php on line 412 Fatal error: Call to a member function make() on null in **PATH_TO_PROJECT**\vendor\illuminate\pagination\LengthAwarePaginator.php on line 90

What I have to do to make it work ?


Can you try this:

{{ posts.links }}

I presume that links is a getter that returns links. If not, this won't work like you expect.


First, you need to include illuminate/pagination in your project (it's not included with illuminate/database):

composer require illuminate/pagination

Now paginator needs to know how to resolve current page. You should make sure this is done before using paginator, I personally put it where I'm setting up dependencies:

// $container is application's DIC container. // Setup Paginator resolvers Illuminate\Pagination\Paginator::currentPageResolver(function ($pageName = 'page') use ($container) { $page = $container->request->getParam($pageName); if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { return $page; } return 1; });

Then in your twig template you can output pagination links. But please you should notice that paginator generates some HTML code which needs to be written to output as is so you'll need to tell twig to ignore escaping for links:

{{ posts.links | raw }}


Sorry for the late :

I didn't keep the project, I don't remember exactly how I did, but this : https://github.com/romanzipp/PHP-Slim-Pagination looks like what I did.

$app->get('/posts', function(Request $req, Response $res, $args = []) use ($cache) { $page = ($req->getParam('page', 0) > 0) ? $req->getParam('page') : 1; $limit = 5; // Number of posts on one page $skip = ($page - 1) * $limit; $count = Post::getCount([]); // Count of all available posts return $this->view->render($res, 'post-list.twig', [ 'pagination' => [ 'needed' => $count > $limit, 'count' => $count, 'page' => $page, 'lastpage' => (ceil($count / $limit) == 0 ? 1 : ceil($count / $limit)), 'limit' => $limit, ], // return list of Posts with Limit and Skip arguments 'posts' => Post::getList([ 'limit' => $limit, 'skip' => $skip, ]) ]); });

In template :

{% if pagination.needed %} <div class="ui pagination menu"> {% for i in 1..pagination.lastpage %} <a class="{% if i == pagination.page %}active{% endif %} item" href="?page={{ i }}">{{ i }}</a> {% endfor %} </div> {% endif %} <div class="ui container"> {% for post in posts %} <a class="item"> {# Post contents (title, url, ...) #} </a> {% endfor %} </div>


  • How to upgrade symfony2 version?
  • How to use a huge array in HLSL (error X4505)
  • Multiple Django sites on the same domain - CSRF fails
  • GCC vs Microsoft : Undefined reference to `_chkstk'?
  • What is the dash (“-”) when used with pipe (“|”) in CMD?
  • Symfony2 - Composer class loader instance in controller
  • Java getNodeName and namespaces
  • Testing Private Methods Not Working
  • Bundle install fails - Gem using github url - What is wrong?
  • Doctrine: UniqueEntity with Many-to-Many field
  • How to add an object in my collection by only using add method? [closed]
  • Validity Method for Reference Classes
  • include dlls in visual studio c++ 2008
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Error processing multiple files
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • In-place sed command not working
  • how to set variables in a php include file?
  • How to access EntityManager inside Entity class in EJB3
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Fill an image in a square container while keeping aspect ratio
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Is there a mandatory requirement to switch app.yaml?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • -fvisibility=hidden not passed by compiler for Debug builds
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to get Windows thread pool to call class member function?
  • Linking SubReports Without LinkChild/LinkMaster
  • Bitwise OR returns boolean when one of operands is nil
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Reading document lines to the user (python)