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
UnionPaginator Documentation
Introduction
The UnionPaginator
package enables you to paginate and unify results from multiple Eloquent models into a single dataset. By merging multiple model queries, it allows for straightforward pagination and sorting of data drawn from various sources.
Key Features:
- Unite and paginate multiple Eloquent model results in one go.
- Apply per-model filters (scopes) before the union.
- Choose between retrieving actual Eloquent models or working with raw database records.
- Mitigate N+1 queries by loading models in bulk.
- Customize selected columns for each model type.
Installation
Install via Composer:
Migration Guide
If you are upgrading from an earlier version of UnionPaginator
, please refer to the Migration Guide for detailed instructions on updating your code to take advantage of the latest features and improvements.
Getting Started
Initializing the UnionPaginator
Specify which Eloquent models you want to combine:
All provided classes must be subclasses of Illuminate\Database\Eloquent\Model
.
Paginating Data
Call paginate
to get paginated results:
This returns a LengthAwarePaginator
instance, seamlessly integrating with Laravel’s pagination utilities.
Applying Scopes to Individual Models
You can apply specific query conditions to a single model type before creating the union:
Customizing Mass Model Retrieval
The UnionPaginator class allows you to customize how models are retrieved during pagination. This can be useful if you need to apply specific logic or optimizations when fetching models from the database.
Registering a Custom Retrieval Callback
You can register a custom callback for retrieving models by type using the fetchModelsUsing method. This method allows you to define how models should be fetched for a specific model type.
Now only active users are included in the final union.
Important Considerations
- Model Type Registration: Ensure that the model type you are registering a callback for has been added to the UnionPaginator instance using the constructor or addModelType method.
- Callback Signature: The callback should accept an array of IDs and return a collection of models. You can use Eloquent's findMany method or any other custom logic to retrieve the models.
- Default Behavior: If no custom callback is registered for a model type, the UnionPaginator will use the default retrieval logic, which is to call findMany on the model type.
By using custom retrieval callbacks, you can optimize and tailor the model fetching process to suit your application's specific needs.
Transforming Results
Use transformResultsFor
to alter records for a particular model type:
If model retrieval is active, $user
is an Eloquent model. If you call preventModelRetrieval()
, $user
is a raw database record (stdClass
).
Preventing Model Retrieval
If you don’t need Eloquent models and prefer raw records:
Transformations still apply, but are run on raw records.
Selecting Columns
Choose specific columns for each model type to reduce overhead:
Soft Deletes
Models using SoftDeletes
are automatically filtered so that soft-deleted records do not appear.
Methods
-
forModels(array $modelTypes): self
Set the models to combine. Throws an exception if a non-model class is provided. -
applyScope(string \$modelType, Closure $callable): self
Modify queries for an individual model type. -
transformResultsFor(string \$modelType, Closure $callable): self
Apply transformations to either models or raw records of a particular model type. -
preventModelRetrieval(): self
Skip loading actual models. Return raw database rows instead. -
setSelectedColumns(string \$modelType, array $columns): self
Specify which columns to fetch for each model type. -
*paginate(\$perPage = 15, \$columns = [''], \$pageName = 'page', $page = null): LengthAwarePaginator**
Execute the union query, apply scopes and transformations, and return a paginator. - __call(\$method, $parameters)
Forward method calls to the underlying union query builder, enabling sorting and other query modifications.
Example Usage
Advanced Usage
Ordering and Complex Queries
You can chain Eloquent methods before paginate()
:
or
Multiple Transformations for the Same Model
Applying multiple transformations for the same model type overwrites earlier ones:
The latter transformation takes precedence.
Handling Empty Results
If no matching records are found, the paginator returns an empty result set without errors.
Testing
UnionPaginator
is well-tested across various scenarios, including:
- Multiple model unions.
- Soft deletes handling.
- Both raw and model-based transformations.
- Large datasets and edge cases.
All versions of laravel-union-paginator with dependencies
illuminate/contracts Version ^10.0|^11.0|^12.0
illuminate/database Version ^10.0|^11.0|^12.0
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/pagination Version ^10.0|^11.0|^12.0