4

I have a parent table and a child table with the eloquent models set up as:

class Parent extends Eloquent {

    public function children()
    {
        return $this->hasMany('child');
    }

}

class Child extends Eloquent {

    public function parent()
    {
        return $this->belongsTo('parent');
    }

}

How can I return only parent rows that have 1 or more children?

2 Answers 2

8

From documentation You do it like this :

$parents = Parent::has('children')->get();
0

I just had one like this, where I was listing children from a parent, but I only wanted to list a child if it also had children (grandChildren).

originally:

$parent->children

didn't work:

$parent->children->has('grandChildren')->get()

worked:

$parent->children()->has('grandChildren')->get()

Note the inclusion of ().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.