Download the PHP package reedware/laravel-composite-relations without Composer
On this page you can find all versions of the php package reedware/laravel-composite-relations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-composite-relations
Laravel Composite Relations
This package adds the ability to have multiple foreign keys in a relation.
Introduction
Eloquent does not natively support using composite keys in relationships. While single key relationships are typically preferred, there are times where they are unfortunately needed, and there's good way around it. This package offers a solution where you can use composite keys, and everything still feels like it's Eloquent.
This package will allow you to define the following composite relations:
- Belongs To
- Has One
- Has Many
All composite relations support eager loading and existence queries (e.g. "where has").
There currently is no intention to add support for additional relations, as these should be enough for the vast majority of use cases.
Installation
Using Composer
Versioning
This package is maintained with the latest version of Laravel in mind, but support follows Laravel's Support Policy.
Package | Laravel | PHP |
---|---|---|
4.x | 10.x - 11.x | 8.1 - 8.3+ |
3.x | 8.x - 10.x | 7.2 - 8.0+ |
2.x | 6.x - 8.x | 7.2 - 8.0+ |
1.x | 5.5 - 5.8 | 7.1 - 7.3+ |
Code Changes
This package does not use a service provider or facade, but rather a trait. On your base model instance, you'll want to include the following:
Usage
1. Defining Relations
Composite Belongs To
Imagine you were defining a non-composite belongs to relation:
Since composite relations use multiple keys, you'll simply define the keys as an array:
Composite Has One
This follows the same structure as the composite belongs to relation:
Composite Has Many
The pattern continues:
2. Omitting Foreign and Local Keys
With non-composite relationships, you aren't actually required to provide the foreign and local key, assuming you follow a certain convention. This functionality is also available for composite relations, but must be defined differently. Here's how:
3. Joining through Composite Relations
This package is compatible with reedware/laravel-relation-joins, meaning you can join through composite relations just like anything else:
You must separately include reedware/laravel-relation-joins for this to work.
4. Using composite and
glue
The default glue between composite keys is 'or'
. Meaning your query will be like:
You can change that by passing 'and'
for the glue parameter:
Giving this result :
All versions of laravel-composite-relations with dependencies
illuminate/database Version ^10.48|^11.0
illuminate/support Version ^10.48|^11.0