LaravelPackages.net
Acme Inc.
Toggle sidebar
sheldonreiff/laravel-advanced-relationships

Multi-column and JSON relationships using awobaz/compoships and staudenmeir/eloquent-json-relations

2.877
4
v1.0.0
About sheldonreiff/laravel-advanced-relationships

sheldonreiff/laravel-advanced-relationships is a Laravel package for multi-column and json relationships using awobaz/compoships and staudenmeir/eloquent-json-relations. It currently has 4 GitHub stars and 2.877 downloads on Packagist (latest version v1.0.0). Install it with composer require sheldonreiff/laravel-advanced-relationships. Discover more Laravel packages by sheldonreiff or browse all Laravel packages to compare alternatives.

Last updated

Laravel Advanced Relationships

Multi-column and JSON-column relationships for Laravel Eloquent using awobaz/compoships and staudenmeir/eloquent-json-relations

In a recent project, I've encountered the need to use both multi-column and JSON-column relationships in the same model. Neither functionality is supported natively by Laravel. Two third-party packages exist that provide these functionalities separately, however, they have several conflicting methods so they can't be used together in the same class. This package combines awobaz/compoships and staudenmeir/eloquent-json-relations to provide full functionality of both in the same Laravel model.

Installation

composer require sheldonreiff/laravel-advanced-relationships

Usage

Add the HasAdvancedRelationships trait to both the parent and the related model. For all of the supported relationships, either a JSON column, multiple columns, or multiple JSON columns may be specified for the local and foreign keys as seen in the example below.

namespace App;

use Illuminate\Database\Eloquent\Model;

class A extends Model
{
    use \Reiff\AdvancedRelationships\HasAdvancedRelationships;
    
    public function b()
    {
        return $this->hasMany('B', 'f1', 'details->l1');
    }
    
    public function c()
    {
        return $this->hasMany('C', ['f1', 'details->f2'], ['details->l1', 'l2']);
    }
}

Supported relationships

Both multi-column and JSON-column support are provided for the following relationships.

  • hasOne
  • hasMany
  • belongsTo

Additional information

Refer to awobaz/compoships and staudenmeir/eloquent-json-relations for additional functionality and other information. This package depends on these packages and simply implements the necessary integrations to make them work together.

Comments