toanld/multi-relationships

此包最新版本(v1.1.3)没有可用的许可证信息。

Compoships 提供了在 Laravel 的 Eloquent ORM 中基于两个(或更多)列指定关系的功能。在处理第三方或现有的模式/数据库时,常常需要在对 Eloquent 关系定义中进行多列匹配。

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

This package is auto-updated.

Last update: 2024-09-05 09:00:48 UTC


README

使用 composer 安装此包。建议仅将此包用于开发。

composer require toanld/multi-relationships

语法

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']);
    }
}

示例

    $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');
    }