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 ?
First, you need to include illuminate/pagination in your project (it's not included with illuminate/database):
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:
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:
Can you try this:
I presume that
links
is a getter that returns links. If not, this won't work like you expect.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.
In template :