Oscar John
Oscar John

Reputation: 41

How do I solve this Pagination Error with Laravelapi

I am trying to send posts to Laravel Api. and I want to paginate them and also arrange them according to the latest. But I am gettting an error Method Illuminate\Database\Eloquent\Collection::paginate does not exist. in file

Here is my Api Code

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

Kindly help. Thank you

Upvotes: 0

Views: 29

Answers (1)

Kevin Bui
Kevin Bui

Reputation: 3045

You don't need to call the get method, this is enough:

$posts = Post::with('comment', 'user')
    ->latest()
    ->paginate(5);

Upvotes: 1

Related Questions