php - Laravel SQL missing data from Join table -
i'm using following code create query join.
$query = rs_vehicles::select( 'rs_vehicles.id', 'rs_vehicles.type', 'rs_vehicles.make', 'rs_vehicles.model', 'rs_vehicles.year', 'rs_vehicles.status', 'rs_vehicle_regs.registration' ); $query ->leftjoin('rs_vehicle_regs', function($join){ $join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id') ->where('rs_vehicle_regs.registration_date', '=', db::raw("(select max(registration_date) rs_vehicle_regs rs_vehicles_id = rs_vehicles.id)")); }); $rsgrid = $query->get();
this produces following sql statement:
select rs_vehicles.id, rs_vehicles.type, rs_vehicles.make, s_vehicles.model, rs_vehicles.year, rs_vehicles.status, s_vehicle_regs.registration rs_vehicles left join rs_vehicle_regs on rs_vehicle_regs.rs_vehicles_id = rs_vehicles.id , rs_vehicle_regs.registration_date = (select max(registration_date) rs_vehicle_regs rs_vehicles_id = rs_vehicles.id)
the sql statement generated laravel runs perfect when execute in mysql workbench , brings expected values all fields. when execute code in laravel although script runs no errors , brings required data 'rs_vehicle_regs.registration' field in joined table comes null. i've spent ages trying figure out , getting nowhere. idea why wont work in laravel? i'm @ wits end.
i have tried changing get() tosql() , following
select `rs_vehicles`.`id`, `rs_vehicles`.`type`, `rs_vehicles`.`make`, `rs_vehicles`.`model`, `rs_vehicles`.`year`, `rs_vehicles`.`vehicle_status`, `vh_type`.`display_text` `type_text`, `vh_status`.`display_text` `vehicle_status_text`, `rs_vehicle_regs`.`registration` `registration` `rs_vehicles` left join `app_params` `vh_type` on `rs_vehicles`.`type` = `vh_type`.`list_value` , `vh_type`.`param_type` = ? left join `app_params` `vh_status` on `rs_vehicles`.`vehicle_status` = `vh_status`.`list_value` , `vh_status`.`param_type` = ? left join `rs_vehicle_regs` on `rs_vehicle_regs`.`rs_vehicles_id` = `rs_vehicles`.`id` `rs_vehicles`.`status` = ?
i have tried following:
$query ->leftjoin('rs_vehicle_regs', function($join){ $join->on('rs_vehicle_regs.rs_vehicles_id', '=', 'rs_employees.rs_vehicles_id') ->where('rs_vehicle_regs.registration_date', '=', 'max(registration_date)'); });
this produces exact same result original query object field values null.
then tried this:
$query ->leftjoin('rs_vehicle_regs', 'rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id');
this join works , brings data looking if join table has more 1 entry related parent table can happen records returned , want 1 record max(registration_date). has me totally stumped.
Comments
Post a Comment