Laravel Recursive Relationships

后端 未结 7 2054
醉梦人生
醉梦人生 2020-11-28 21:22

I\'m working on a project in Laravel. I have an Account model that can have a parent or can have children, so I have my model set up like so:

public         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 21:31

    I've created a package that uses common table expressions (CTE) to implement recursive relationships: https://github.com/staudenmeir/laravel-adjacency-list

    You can use the descendants relationship to get all children of an account recursively:

    class Account extends Model
    {
        use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
    }
    
    $allChildren = Account::find($id)->descendants;
    

提交回复
热议问题