reedware / laravel-composite-relations
Adds the ability define composite eloquent relations.
Installs: 38 201
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 1
Forks: 3
Open Issues: 0
Requires
- php: ^8.1
- illuminate/database: ^10.48|^11.0
- illuminate/support: ^10.48|^11.0
Requires (Dev)
- laravel/pint: ^1.5
- mockery/mockery: ^1.6.6
- php-coveralls/php-coveralls: ^2.4
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- reedware/laravel-relation-joins: ^6.0
- symfony/var-dumper: ^6.4.4|^7.0
README
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
composer require reedware/laravel-composite-relations
Versioning
This package is maintained with the latest version of Laravel in mind, but support follows Laravel's Support Policy.
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:
use Illuminate\Database\Eloquent\Model as Eloquent; use Reedware\LaravelCompositeRelations\HasCompositeRelations; abstract class Model extends Eloquent { use HasCompositeRelations; }
Usage
1. Defining Relations
Composite Belongs To
Imagine you were defining a non-composite belongs to relation:
public function myRelation() { return $this->belongsTo(MyRelated::class, 'my_column_on_this_table', 'my_column_on_the_other_table'); }
Since composite relations use multiple keys, you'll simply define the keys as an array:
public function myCompositeRelation() { return $this->compositeBelongsTo(MyRelated::class, ['my_first_key', 'my_second_key'], ['their_first_key', 'their_second_key']); }
Composite Has One
This follows the same structure as the composite belongs to relation:
public function myCompositeRelation() { return $this->compositeHasOne(MyRelated::class, ['their_first_key', 'their_second_key'], ['my_first_key', 'my_second_key']); }
Composite Has Many
The pattern continues:
public function myCompositeRelation() { return $this->compositeHasMany(MyRelated::class, ['foreign_1', 'foreign_2'], ['local_1', 'local_2']); }
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:
class Task extends Models { /** * The primary keys for the model. * * @var array */ protected $primaryKeys = ['vendor_id', 'vendor_name']; public function importSummary() { return $this->compositeHasOne(TaskImportSummary::class); } } class TaskImportSummary extends Models { public function task() { return $this->compositeBelongsTo(TaskImportSummary::class); } }
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:
$task->joinRelation('importSummary', function($join) { $join->where('task_import_summaries.name', 'like', '%Relation joins are cool!%'); });
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:
where (("foreign_1" = ? or "foreign_2" = ?) or ("foreign_1" = ? or "foreign_2" = ?))
You can change that by passing 'and'
for the glue parameter:
public function myCompositeBelongsToRelation() { return $this->compositeBelongsTo(MyRelated::class, ['local_1', 'local_2'], ['foreign_1', 'foreign_2'], null, 'and'); } public function myCompositeHasOneRelation() { return $this->compositeHasOne(MyRelated::class, ['foreign_1', 'foreign_2'], ['local_1', 'local_2'], 'and'); } public function myCompositeHasManyRelation() { return $this->compositeHasMany(MyRelated::class, ['foreign_1', 'foreign_2'], ['local_1', 'local_2'], 'and'); }
Giving this result :
where (("foreign_1" = ? and "foreign_2" = ?) or ("foreign_1" = ? and "foreign_2" = ?))