Download the PHP package austinw/laravel-union-paginator without Composer
On this page you can find all versions of the php package austinw/laravel-union-paginator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download austinw/laravel-union-paginator
More information about austinw/laravel-union-paginator
Files in austinw/laravel-union-paginator
Package laravel-union-paginator
Short Description Combines data from multiple models into a single unified query using SQL unions, allowing for consistent pagination and customization across diverse data sources.
License MIT
Homepage https://github.com/austinw/laravel-union-paginator
Informations about the package laravel-union-paginator
Laravel Union Paginator
Paginate several Eloquent models as one list — an activity feed of posts, comments and users, for example — with a single SQL UNION, and get the models back in order.
Installation
Requires PHP 8.2+ and Laravel 12 or 13. Upgrading from 2.x? See the migration guide.
How it works
- Each model becomes one
SELECTof four columns:id(the primary key),created_at,updated_atandtype(the model's class name). Global scopes and soft deletes apply. - The selects are combined with
UNIONand wrapped in a derived table,union_paginator. - Your
whereandorderBycalls run on that derived table, so they apply to every model. - The page of rows is turned back into models with one
findMany()per model type.
Filtering and ordering the whole union
Query builder where* and orderBy* methods, latest(), oldest(), reorder() and inRandomOrder() are forwarded to the union. They can only see the union's columns (id, created_at, updated_at, type, plus any you select).
When you order, type and id are appended as tie-breakers so pages stay stable when sort values are equal. Without an order, the database decides the row order — add one if the order matters.
Calls can come in any order: the query is built when you paginate. when() and unless() receive the paginator:
where(fn ($query) => ...) closures receive a plain query builder for the derived table, not an Eloquent builder.
Constraining one model
Use applyScope() for conditions on a single model, including relationship queries such as whereHas():
The callback receives that model's Eloquent builder. Call applyScope() more than once to add more conditions.
Pagination styles
Cursor pagination needs column orders on non-null columns. type and id are appended automatically, so ->latest()->cursorPaginate() works as long as created_at is never null. Raw (orderByRaw), random and expression orders throw, because Laravel cannot build a cursor from them.
Loading models
By default each page runs one findMany() per model type. To eager load or customise the query, use fetchModelsUsing():
The callback receives the keys on the current page and returns the models. Rows whose model is not returned (for example, deleted between the two queries) are left out of the page.
Raw rows
Skip model loading and get the union rows (stdClass) instead:
Transforming results
The callback receives the model, or the raw row when model retrieval is prevented. A later call for the same model replaces the earlier one.
Custom columns
Replace a model's default select with setSelectedColumns(). Columns in a UNION are matched by position, so every model must select the same number of columns in the same order. Alias the key as id. The paginator always adds type as the last column.
Models without timestamp columns should set const CREATED_AT = null; and const UPDATED_AT = null; — the paginator then selects NULL for them.
Limitations
- All models must use the same database connection.
- Primary keys must have compatible types across models (Postgres will not union an integer with a UUID). Cast them with
setSelectedColumns()if needed.
Testing
CI runs the suite on PHP 8.2–8.5 with Laravel 12 and 13 on SQLite, plus MySQL 8.4 and Postgres 17.
License
MIT. See LICENSE.md.
All versions of laravel-union-paginator with dependencies
illuminate/contracts Version ^12.0|^13.0
illuminate/database Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
illuminate/pagination Version ^12.0|^13.0