eloquent-has-many-deep
eloquent-has-many-deep copied to clipboard
Usage with laravel-adjacency-list?
Is this library compatible with laravel-adjacency-list?
I'm trying the following code but the relationship returns an empty array:
class Category extends Model
{
use HasFactory;
use HasRecursiveRelationships;
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
...
public function directSpecifications()
{
return $this->belongsToMany(
Specification::class,
CategorySpecification::class
);
}
public function specifications()
{
return $this->hasManyDeepFromRelations(
$this->newRelatedInstance(Category::class)->ancestorsAndSelf(),
$this->newRelatedInstance(Category::class)->directSpecifications()
);
}
}
Unfortunately, this kind of combination is not possible because recursive relationships can't be concatenated like "normal" relationships.
What you need instead is a BelongsToManyOfAncestors
relationship (equivalent to BelongsToManyOfDescendants
) and I'm working on that. I'll give you an update when the relationship is ready.
OK, thanks!
Any news on the progress @staudenmeir? I came across the same problem :/
I just released new versions for both packages that add support for relationships like yours.
You only need to remove the first ->newRelatedInstance(Category::class)
:
class Category extends Model
{
public function specifications()
{
return $this->hasManyDeepFromRelations(
$this->ancestorsAndSelf(),
$this->newRelatedInstance(Category::class)->directSpecifications()
);
}
}