Sidney Sousa
Sidney Sousa

Reputation: 3584

Laravel paginate not working using with query

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?

Upvotes: 0

Views: 788

Answers (1)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

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

Post::with('user', 'category')->paginate(2);

Upvotes: 2

Related Questions