Skip to content

Sorting a HasManyOfDescendants relationship based on the CTE #214

Discussion options

You must be logged in to vote

Hi @jordyvanderhaegen,
There's a newer and better way to define relationships like recursiveProducts() that I would recommend for your case:
https://github.com/staudenmeir/laravel-adjacency-list#concatenation

use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

class Category extends Model
{
    use HasRelationships; // Requires staudenmeir/eloquent-has-many-deep
    use HasRecursiveRelationships;

    public function recursiveProducts()
    {
        return $this->hasManyDeepFromRelations($this->descendantsAndSelf(), (new static)->products());
    }

    public function products()
    {
        return $this->hasMany

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jordyvanderhaegen
Comment options

Answer selected by jordyvanderhaegen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants