Is it possible to have a HasManyThrough relationship that's deeper than two levels?

后端 未结 2 1955
日久生厌
日久生厌 2021-01-20 00:54

I have the following models:

Product
BaseProduct
BaseCode
Series

Each of them is something of a separate entity, but they\'re related.

相关标签:
2条回答
  • 2021-01-20 01:38

    I am somewhat new to 4.1, but it looks as though hasManyThrough() was not designed for the types of relationships you are looking for and is really just a shortcut for two level deep eager loading.

    Try traditional eager loading instead...

    $data = Product::with('BaseProduct.BaseCode.Series');

    You will need to make sure the relationships are setup in your models as necessary and instead of calling model names, you will want to call the methods that are defining the relationships.

    Also obviously, be sure your models know what the primary keys are by using protected $primaryKey = 'SeriesID' etc...

    0 讨论(0)
  • 2021-01-20 01:42

    I created a HasManyThrough relationship with unlimited levels: Repository on GitHub

    After the installation, you can use it like this:

    class Series extends Model {
        use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
    
        public function products() {
            return $this->hasManyDeep(Product::class,
                [BaseCode::class, BaseProduct::class],
                ['SeriesId', 'BaseCodeId', 'BaseProductId']
            );
        }
    }
    
    0 讨论(0)
提交回复
热议问题