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

insight's avatar

How to write a query within another query ?

Dear friends,

My problem is i have 3 tables with some relation. But I need a logic which write in the logic code below

 
 $tr_req_data = TransferRequest::with(['stations' => function ($qry) {

                                      $qry->with(['localbody' => function ($qrylocalbody) {
                                        $qrylocalbody->where("stations.requested_station","=","localbody.lbid");

                                      }]);

                                  }])->paginate(5);

But I got some exception like


"message": "SQLSTATE[42P01]: Undefined table: 7 ERROR:  missing FROM-clause entry for table \"ulbgt_stations\"\nLINE 1: ...e \"ulbgt_local_bodies\".\"lbid\" in (, , ) and \"ulbgt_sta...\n                                                             ^ (SQL: select * from \"ulbgt_local_bodies\" where \"ulbgt_local_bodies\".\"lbid\" in (1, 2, 3) and \"ulbgt_stations\".\"requested_station\" = localbody.lbid)",

Please advise how this concept can do?

Thanks

0 likes
4 replies
insight's avatar

No Alamin, It's not about left join concept. My doubt is how to implement above problem . It has no significance with left join..

Thanks

insight's avatar

Hi Renoirfaria,

Your point is near to my doubt but not touch my specific need . Do you please just try to write a code

My TransferRequest table has a Primary Key Id which is a foreign key in 'Stations' model. In stations model there is a field 'requested_station' which is the 'lbid' field of 'localbody' model.

In that situation on iterating the value of model 'station' we find the corresponding filed of 'localbody' model...

please try any body

Thanks

Please or to participate in this conversation.