Download the PHP package staudenmeir/laravel-merged-relations without Composer
On this page you can find all versions of the php package staudenmeir/laravel-merged-relations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download staudenmeir/laravel-merged-relations
More information about staudenmeir/laravel-merged-relations
Files in staudenmeir/laravel-merged-relations
Package laravel-merged-relations
Short Description Merged Laravel Eloquent relationships
License MIT
Informations about the package laravel-merged-relations
Laravel Merged Relations
This Laravel Eloquent extension allows merging multiple relationships using SQL views.
The relationships can target the same or different related models.
Supports Laravel 5.5+.
Installation
composer require staudenmeir/laravel-merged-relations:"^1.0"
Use this command if you are in PowerShell on Windows (e.g. in VS Code):
composer require staudenmeir/laravel-merged-relations:"^^^^1.0"
Versions
Laravel | Package |
---|---|
11.x | 1.9 |
10.x | 1.6 |
9.x | 1.5 |
8.x | 1.4 |
7.x | 1.3 |
6.x | 1.2 |
5.8 | 1.1 |
5.5–5.7 | 1.0 |
Usage
- Use Cases
- Step 1: Creating Views
- Step 2: Defining Relationships
- Pivot Table Data
- Limitations
- Testing
Use Cases
Use the package to merge multiple polymorphic relationships:
Or use it to merge relationships with different depths:
Step 1: Creating Views
Before you can define the new relationship, you need to create the merge view in a migration:
By default, the view doesn't remove duplicates. Use createMergeViewWithoutDuplicates()
to get unique results:
You can also replace an existing view:
The package includes staudenmeir/laravel-migration-views. You can use its methods to rename and drop views:
If you are using php artisan migrate:fresh
, you can drop all views with --drop-views
.
Step 2: Defining Relationships
With the view created, you can define the merged relationship.
Use the HasMergedRelationships
trait in your model and provide the view name:
If all original relationships target the same related model, you can use mergedRelationWithModel()
. This allows you to
access local scopes and use methods like whereHas()
or withCount()
:
You can use the merged relationship like any other relationship:
Pivot Table Data
You can retrieve additional pivot columns if your merge view consists of many-to-many relationships.
Add the desired pivot columns to all relationships:
Limitations
In the original relationships, it's currently not possible to limit the selected columns or apply withCount()
.
In the merged relationships, it's not possible to remove global scopes like SoftDeletes
. They can only be removed in
the original relationships.
Testing
If you use PHPUnit or a similar tool to run tests, add this property to your base test class to ensure that database views are dropped when the test database is cleaned up:
Contributing
Please see CODE OF CONDUCT for details.
All versions of laravel-merged-relations with dependencies
illuminate/database Version ^11.0
staudenmeir/laravel-migration-views Version ^1.9.1