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
CodePudding user response:
You don't need to call the get
method, this is enough:
$posts = Post::with('comment', 'user')
->latest()
->paginate(5);