2

I'm using the following code to create a query with a 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) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)"));
                });       

    $rsGrid =   $query->get();

This produces the 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 from rs_vehicles 
left join rs_vehicle_regs 
on rs_vehicle_regs.rs_vehicles_id = rs_vehicles.id 
and rs_vehicle_regs.registration_date = (select max(registration_date) from rs_vehicle_regs where rs_vehicles_id = rs_vehicles.id)

The SQL statement generated by Laravel runs perfect when I execute it in MySQL Workbench and it brings back the expected values for ALL fields. However when I execute the code in Laravel although the script runs perfectly with no errors and brings back almost all the required data the 'rs_vehicle_regs.registration' field which is in the joined table comes back as null. I've spent ages trying to figure this out and I am getting nowhere. Anybody any idea why this wont work in Laravel? I'm at my wits end.

I have tried changing get() to toSql() and I get the 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` as `type_text`, `vh_status`.`display_text` as `vehicle_status_text`, `rs_vehicle_regs`.`registration` as `registration` from `rs_vehicles` left join `app_params` as `vh_type` on `rs_vehicles`.`type` = `vh_type`.`list_value` and `vh_type`.`param_type` = ? left join `app_params` as `vh_status` on `rs_vehicles`.`vehicle_status` = `vh_status`.`list_value` and `vh_status`.`param_type` = ? left join `rs_vehicle_regs` on `rs_vehicle_regs`.`rs_vehicles_id` = `rs_vehicles`.`id` where `rs_vehicles`.`status` = ?

I have also tried the 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 the exact same result as the original query where the object as the field but the values are null.

Then I tried this:

$query      ->leftJoin('rs_vehicle_regs', 'rs_vehicle_regs.rs_vehicles_id', '=', 'rs_vehicles.id');

This join works and brings back the data I am looking for however if the join table has more than one entry related to the parent table as can happen I get all records returned and I only want one record were max(registration_date). This has me totally stumped.

8
  • This is an interesting mystery. Are you certain that the Laravel query was running on exactly the same database as when you ran the raw query on MySQL? Is the entire rs_vehicle_regs.registration null or just a portion of it? Commented Jul 12, 2016 at 14:21
  • I don't think you can get another table's column when you query the DB via a specific table model. It would be problematic if you subsequently called $result->save() Commented Jul 12, 2016 at 14:25
  • Yes i'm 100% certain that the Laravel query is running on exactly the same database as the raw query on MySQL and the entire rs_vehicle_regs.registration is null not just a portion of it. For some reason Laravel doesn't seem to be putting the data from the join table into the resulting object. If I dd($rsGrid) and view the object the field is there it is just null. Commented Jul 12, 2016 at 14:34
  • Could be similar to this question: stackoverflow.com/a/18120728/2526914 - try flipping the query order of the code... Commented Jul 12, 2016 at 14:42
  • Tried flipping the query order as suggested. Afraid it didn't make any difference. I think it might have something to do with the DB::raw() part of the query. If I simplify the join and remove this part it return all the values as you would expect however I seriously need to bring back only the record with the max(registration_date) Commented Jul 12, 2016 at 15:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.