0

There are lot available reference that I went through before asking the question. And it seems all of them were just one to two layer relationship only. While my case is different because it can have n layer of relationship, so it's not really helpful in my end.


I have here nested relationship of Person.

class Person extends Model
{
    public function children()
    {
        return $this->hasMany(Person::class, 'parent_id', 'id')->with('children');
    }
}

This would result to this sample Model structure data. (show snippet below)

[
    {
        name: 'Don Pepe',
        gender: 'male',
        children: [
            {
                name: 'Juan',
                gender: 'male',
                children: [
                    {
                        name: 'Vito',
                        gender: 'male',
                        children: ... and so on, so forth
                    },
                    ...
                ],
            },
            ...
        ],
    },
    ...
]

I would like to ask how to filter these data. For example like, if I would like to filter the male gender? We could just simply where clause the parent but for children of children, how we gonna where clause it?

I doubt it could not be done in Laravel Eloquent, only in collection. Please let me know your thoughts.

Laravel nested relationships https://laracasts.com/discuss/channels/laravel/eloquent-nested-relations-with-where-clause

tempra
  • 2,455
  • 1
  • 12
  • 27

0 Answers0