Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent return rows where relationship exists

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?

like image 523
James Avatar asked Sep 01 '25 03:09

James


1 Answers

From documentation You do it like this :

$parents = Parent::has('children')->get();
like image 143
webNeat Avatar answered Sep 02 '25 15:09

webNeat