Specify which fields you want to retrieve :
$query = $this->model->select('areas.*');
$query->join('addresses','areas.id','=','addresses.area_id');
$query->orderBy( ['id','desc'] );
But why join then eager loading ??
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a simple select query which eager loads associated data. I put a join on the initial select so I could better search the data but it causes incorrect relationship data to be retrieved. The queries being generated are as follows
CORRECT WITHOUT JOIN
select * from `areas` order by `name` asc limit 10 offset 0
select * from `addresses` where `addresses`.`area_id` in ('100', '16', '7', '81', '37', '76', '55', '43', '86', '11')
INCORRECT WITH JOIN ON INITIAL SELECT
select * from `areas` inner join `addresses` ON `areas`.`id` = `addresses`.`area_id` order by `name` asc limit 10 offset 0
select * from `addresses` where `addresses`.`area_id` in ('110', '26', '17', '91', '47', '86', '65', '53', '96', '21')
Basically, I want to search through the "address" details when retrieving "area" details. If you look closely when a join is added the value "100" from the first query, which is the primary key of the area row is swopped out for "110" which is the primary key of the related address row.
A simplified version of my query reads as follows
$query = $this->model->orderBy( ['id','desc'] );
$query->join('addresses','areas.id','=','addresses.area_id');
$query->with('address');
// Paginate and return the results.
return $query->paginate($pagination);
Specify which fields you want to retrieve :
$query = $this->model->select('areas.*');
$query->join('addresses','areas.id','=','addresses.area_id');
$query->orderBy( ['id','desc'] );
But why join then eager loading ??
Please or to participate in this conversation.