A code with Error:
$posts = BlogPost::with('comment', 'user')
->latest()
->get()
->paginate(5);
return response()->json([
'status'=>200,
'posts'=>$posts
]);
In above code a Developer is trying to access records using Laravel Eloquent, however He is trying to use both ->get()
as well as ->paginate()
method at the same time.
You should not do that, when we use paginate method to it handle get operations too, the only difference is that it process the records with LIMIT as well few additional properties like number of pages, records current page etc.
So, to fix this issue all you have to is remove the get()
method from the call as showing below:
$posts = BlogPost::with('comment', 'user')
->latest()
->paginate(5);
return response()->json([
'status'=>200,
'posts'=>$posts
]);
Now you should be good to go.