Filtering Development Tutorials, Guides & Insights
Unlock 2+ expert-curated filtering tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your filtering skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Tutorial
php
Building a Custom Pagination System for API Responses
public function index(Request $request)
{
$limit = $request->get('limit', 10);
$cursor = $request->get('cursor');
$query = Post::query();
if ($cursor) {
$query->where('id', '>', $cursor); // Fetch items after the cursor
}
$posts = $query->orderBy('id')->take($limit + 1)->get();
$nextCursor = $posts->count() > $limit ? $posts->last()->id : null;
return response()->json([
'data' => $posts->take($limit), // Return only the requested number of items
'meta' => [
'limit' => $limit,
'next_cursor' => $nextCursor,
],
]);
} {
"data": [
{ "id": 11, "title": "Post 11" },
{ "id": 12, "title": "Post 12" }
],
"meta": {
"limit": 10,
"next_cursor": 20
}
}Nov 16, 2024
Read More Cheatsheet
REST API Cheatsheet: Comprehensive Guide with Examples
No preview available for this content.
Aug 24, 2024
Read More