pal3
pal3

Reputation: 241

Slim 3 pagination with Twig view using Eloquent ORM

I'm using Twig view for my slim 3 application but I don't know how to make pagination using the eloquent ORM below is my code.

MODEL:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Todo extends Model
{
    protected $table = "todo";
    protected $fillable = [
        'todo_name',
    ];
}

and this is my code to render a view

use App\Models\Todo;
$app->get('/', function ($request, $response) {
    $data = Todo::all()->paginate(5);
    return $this->view->render($response, 'home.twig', [
        'title' => 'Home',
        'todolist' => $data,
    ]);
})->setName('homepage');

and I got this error

Method paginate does not exist.

Upvotes: 2

Views: 944

Answers (2)

Nima
Nima

Reputation: 3409

To use pagination you need illuminate/pagination package. It is not included by default when you include illuminate/database. You can use composer to include it in your project:

composer require illuminate/pagination

And you should not call paginate() method after a call to all() or get(). Try this instead:

$data=Todo::paginate(5);

And please note, for pagination to work correctly, it needs to know current page number otherwise it will always return results for first page. Please have a look at this answer to see how to setup page resolver.

Upvotes: 0

Lee
Lee

Reputation: 487

Try $data = Todo::paginate(5);

Upvotes: 1

Related Questions