Sensors table
id name
1 Sensor no1
2 Sensor no2
3 Sensor no3
Temperatures table (log table)
temperature sensor_id timestamp
2.85 1 2021-10-19 18:37:34
5.05 2 2021-10-19 18:37:34
2.90 3 2021-10-20 18:37:34
5.65 1 2021-10-21 18:37:34
21.5 3 2021-10-22 18:37:34
In eloquent I have sensor model with relation
public function latestTemperature()
{
return $this->hasOne(Temperature::class)->latestOfMany();
}
I get SQL error that temperatures.id does not exist. – I do not have ID in that table.
How can I tell it to where sensor id = x order by timestamp desc limit 1 or something?
Unknown column 'temperatures.id' 'field list'-s (SQL: select `temperatures`.* from `temperatures` inner join (select MAX(`temperatures`.`id`) as `id_aggregate`, `temperatures`.`sensor_id` from `temperatures` where `temperatures`.`sensor_id` in (1) group by `temperatures`.`sensor_id`) as `latestOfMany` on `latestOfMany`.`id_aggregate` = `temperatures`.`id` and `latestOfMany`.`sensor_id` = `temperatures`.`sensor_id`)