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

I have the following models:

Product
BaseProduct
BaseCode
Series

Each of them is something like a separate entity, but they are interconnected.

Producthas a foreign key for BaseProduct, BaseProducthas a foreign key for BaseCodeand BaseCodehas a foreign key Series.

I have specified a method in my model BaseCode, so I can select all Productsthat apply to this particular one BaseCode:

public function Products()
{
    return $this->hasManyThrough('Product', 'BaseProduct', 'BaseCodeId', 'BaseProductId');
}

BaseCodeIdis PK for BaseCodeand FK in BaseProduct, which points to PK, and BaseProductIdis PK for BaseProduct, and in the table ProductFK, which points to it.

It's good and good, and for me it's just a dandy.

I would like to go one more level and define Seriessomething like the following in my model :

public function Products()
{
    //It here that I'd have no idea what to do - is there something like a three-level deep "hasManyThroughThrough" or something?
    return $this->BaseProducts()-> /* and then whatever I'd do here to get the products */
}

public function BaseProducts()
{
    return $this->hasManyThrough('BaseProduct', 'BaseCode', 'SeriesId', 'BaseCodeId');
}

Product, Series, ?

+4
1

4.1, , hasManyThrough() , , .

...

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

, , .

, , protected $primaryKey = 'SeriesID' ..

+6

Source: https://habr.com/ru/post/1525546/


All Articles