Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hanif-king's avatar

how to get all rows of a child , child with parent id.

i have 3 tables parent, child, and child of child i want to get all row of the third table via parent table. where relationships between parent and child is hasMany and between child of child is hasOne all models are linked properly and they are fine but only my question is how could i get data from my third table please look the below prototype.

----Parent_Table-----
| id                               
| order_name            
| .                                  
| .                                  
--------------------------
----firstChild_Table-----
| id                                
| name                        
| .                                  
| .                                  
--------------------------
----child_of_Child_Table-----
| id                                          
| name                                  
| .                                            
| .                                            
---------------------------------

my models are:

//  in parent table (Order)
  public function owntransaction(){
        return $this->hasMany('App\OwnTransaction','order_id');
    }
// in child table (OwnTransaction)
 public function order(){
        return $this->belongsTo('App\Order');
    }

 public function submitedtransaction(){
        return $this->hasOne('App\SubmitedTransaction','submitedTran_id');
    }

// in child of child table (SubmitedTransaction)
 public function owntransaction(){
        return $this->belongsTo('App\OwnTransaction');
    }

I have only parent ID and i want to get all rows of child of child.

0 likes
1 reply
rawilk's avatar

I would say use a hasManyThrough relationship, but at the same time, why would you need 3 tables? If they're all the same thing, you could accomplish this with only 1 table, all you need is the parent_id column on that table. Any rows with a parent_id of 0 are parents, the rest are children of some other row.

1 like

Please or to participate in this conversation.