DeveloperBreeze

Laravel Programming Tutorials, Guides & Best Practices

Explore 51+ expertly crafted laravel tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

Tutorial
php

Building a Custom Pagination System for API Responses

Add query parameters for filtering:

   public function index(Request $request)
   {
       $title = $request->get('title');
       $query = Post::query();

       if ($title) {
           $query->where('title', 'like', '%' . $title . '%');
       }

       $posts = $query->paginate(10);

       return response()->json([
           'data' => $posts->items(),
           'meta' => [
               'current_page' => $posts->currentPage(),
               'per_page' => $posts->perPage(),
               'total' => $posts->total(),
               'last_page' => $posts->lastPage(),
           ],
           'links' => [
               'next' => $posts->nextPageUrl(),
               'previous' => $posts->previousPageUrl(),
           ],
       ]);
   }

Nov 16, 2024
Read More