0

I can view all my posts and include their respective owner and category in the query.

public function index()
{
    $posts = Post::with('user', 'category')->get();
    return response()->json([
        'posts' => $posts, 
    ], 200);
}

Note: I used the with helper because thr front-end of the site is in vuejs.

Now I want to add pagination in my code but I get the following error:

"Method Illuminate\Database\Eloquent\Collection::with does not exist."

This is what I tried:

$posts = Post::paginate(2)->with('user', 'category')->get();

How can I use the laravel pagination?

Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99

1 Answers1

2

For get the result you must be use paginate, get, first methods the end of the query

Post::with('user', 'category')->paginate(2);
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55