toanld/multi-relationships

There is no license information available for the latest version (v1.1.3) of this package.

Compoships offers the ability to specify relationships based on two (or more) columns in Laravel's Eloquent ORM. The need to match multiple columns in the definition of an Eloquent relationship often arises when working with third party or pre existing schema/database.

v1.1.3 2022-09-05 04:23 UTC

This package is auto-updated.

Last update: 2024-05-05 08:23:48 UTC


README

Require this package with composer. It is recommended to only require the package for development.

composer require toanld/multi-relationships

Syntax

namespace App;

use Illuminate\Database\Eloquent\Model;
use Toanld\Relationship\MultiRelationships;

class Test extends Model
{
    use MultiRelationships;
    
    public function category(){
        //list_cat can be json ids (example: [2,3,43,23]) or string list ids (example: 2,3,43,23)
        return $this->hasOne(Category::class,'id',['cat_3','cat_2','cat_1','list_cat']);
    }
}

Example

    $data = Test::with(['category:id,name'])->limit(2)->get();
    foreach ($data as $row){
        //return model category relate with field cat_1
        $category = $row->getRelationshipValue($row->cat_1,'category');
    }