Maginium
Docs
Docs
  • Welcome
  • Getting Started
    • Quickstart
    • Installation
    • Structure
  • The Basics
    • Concurrency
    • 🔤Strings
    • ⚙️Actions
    • 🗃️Cache
    • 🗂️Collections
    • 🔲Component
    • ⚙️Config
    • 💬Console
    • 🗃️Database
      • Overview
      • Eloquent
      • Seeder
      • Migration
      • Models
      • Factories
      • Schemas
      • Traits
      • Concerns
    • 🛅Container
    • 🔠Enum
    • 📦Event
    • 📝Log
    • 🔢Pagination
    • 💬Prompts
    • 🌐Request
    • 📡Response
    • ⚡Redis
    • 📏Resize
    • 🔌Serializer
      • Json
      • Serializer
      • Closure Serializer
    • 🔑Uuid
    • 👤Avatar
    • 🔐Hashing
    • 📤Dto
      • Overview
      • Attributes
    • 🌍Locale
    • 🔗Token
      • Api Keys
      • Admin Token
      • Customer Token
    • 🕘Message Queue
    • 🖼️Media
    • Helpers
    • 📜Crud
      • Filters and Sorts
        • introduction
        • Basic Usage
        • JS Examples
          • Available Methods
          • Sorts
          • Filters
        • Rename Fields
        • Relation Fields
        • Changing Params Source
        • Allowed Fields
        • Queries and javascript examples
        • Sort null values last
        • Restrict
        • Custom Filters
  • Commerce Modules
    • API Key Module
    • Auth Module
    • Cart Module
    • Customer Module
    • Inventory Module
    • Fulfillment Module
    • Order Module
    • Payment Module
    • Pricing Module
    • Product Module
    • Promotion Module
    • User Module
  • SDKs and Tools
    • create-maginium-app
    • Maginium CLI
    • JS SDK
    • Storefront Starter
Powered by GitBook
On this page
  • Getting Started
  • Using Pagination with Query Builder
  • Pagination Links
  • Customizing Pagination
  • Pagination with Collections
  • API Pagination
  1. The Basics

Pagination

Pagination is a crucial feature in web applications, enabling you to divide large datasets into smaller, more manageable chunks. Maginium offers a powerful and flexible pagination system that integrates seamlessly with the framework's data querying capabilities.


Getting Started

Pagination in Maginium is implemented using the built-in paginator. The paginator can be used with database queries, collections, or any iterable dataset to provide efficient navigation across multiple pages.

Important: Maginium's paginator is optimised for both performance and user experience, offering intuitive methods for customization.


Using Pagination with Query Builder

The query builder's paginate method simplifies database pagination:

use Maginium\Framework\Database\Query;

$users = Query::table('users')->paginate(10);

This generates a paginated result set containing 10 records per page.

Accessing Pagination Data

Once you have a paginated result set, you can access additional pagination data:

$users->currentPage(); // Current page number
$users->lastPage(); // Total number of pages
$users->perPage(); // Items per page
$users->total(); // Total items
$users->items(); // Data for the current page

Pagination Links

The links method generates HTML for pagination controls:

echo $users->links();

Pagination links are automatically styled using Maginium's default CSS classes. You can override these styles for custom designs.


Customizing Pagination

Maginium's paginator allows customization of the number of items per page and query parameters:

$users = Query::table('users')->paginate(15, 'page', ['*'], 'userPage');

Advanced Customization

You can modify the pagination view to meet your application's needs:

$users->setPageName('custom_page');
$users->appends(['filter' => 'active']);

Pagination with Collections

If you're working with collections, you can use the forPage method:

use Maginium\Framework\Support\Collection;

$collection = new Collection(range(1, 100));
$paginated = $collection->forPage(2, 10);

// Returns items 11 to 20

API Pagination

For API responses, Maginium provides JSON structures that include pagination metadata:

return $users->toJson();

The response includes:

  • Data: Current page's dataset

  • Meta: Pagination information (e.g., total items, current page, last page)

Example Response

{
  "data": [
    { "id": 1, "name": "John" },
    { "id": 2, "name": "Jane" }
  ],
  "meta": {
    "current_page": 1,
    "last_page": 10,
    "per_page": 10,
    "total": 100
  }
}

PreviousLogNextPrompts

Last updated 4 months ago

🔢