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 ?
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>