lundi 21 mai 2018

Laravel query nested relationship on same table

I have a users table whose basic structure looks like:

id, uname, parent_id
1 john null
2 smith 1
3 mickey 1
4 lara 2
5 tom 3
6 jessica 4
7 baneet 4

and relationship in user model is defined like :

    public function children()
    {
     return $this->hasMany('App\Models\User', 'parent_id');
    }

    public function parents()
    {
        return $this->belongsTo('App\Models\User', 'parent_id');
    }

So, what I am trying to do is: find all users whose children have no children. In Lehman terms: Find all families who have children but their children don't have any children.

I know how to do it when I just have to query immediate relationship like

User::whereHas('children',function($query){
           // $query;
        })->get();

But not sure how to do in the nested relationship.

Is there any way to achieve this?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2s282we
via IFTTT

Aucun commentaire:

Enregistrer un commentaire