0

I have two tables; dashboard_map_items and device_data_current. in dashboard_map_items I have two field, id and command and in device_data_current I have three field id, commands, current_value. Based on the id and commands in my dashboard_map_items I need get the current_data from device_data_current.

Now I dont know where to start. Before I was able to hard-code the id and command and fetch the current_data from device_data_current but this is not working for me anymore, because the id and commands will be changed in the future.

$currentValues = DeviceDataCurrent::where('map_id', 1)
          ->where('system_id', $system_id)
          ->where('command', 1)->where('id', 64)
          ->orderby('datetime', 'DESC')->take(1)->get();

this is what i was doing to get the data. if it would help i am using laravel 4.2 framework but i dont know it has anything to do with laravel. it is pure php. Any help would be appreciated.

AlbertSm
  • 105
  • 2
  • 2
  • 12
  • FYI I am tying to do it in Controller.php . the above code was giving me the last entry based on the datetime. – AlbertSm Jun 02 '15 at 17:57

1 Answers1

0

I found the solution

$currentValues = DashboardMapItem::where('system_id', $system_id)

->select()->join('device_data_current', function($join){
                  $join->on('device_data_current.id', '=', 'dashboard_map_items.device_id');
                  $join->on('device_data_current.command', '=', 'dashboard_map_items.command');
})
->get();

dd($currentValues->toArray());
AlbertSm
  • 105
  • 2
  • 2
  • 12