I'm running the query which give me the result.Query is given below
Model file
public function units()
{
return $this->hasMany('App\PowerConsumption','device_id','device_id');
}
Controller file
return Device::with('units')->where('user_id',2)->get();
Result of this query is:
[{
device_id: 1,
user_id: 2,
device_name: "Bulb 1",
relay_num: 22,
sensor_num: 4,
status: 1,
created_at: "2016-07-11 02:11:32",
updated_at: "2016-07-19 08:25:30",
units: [
{
p_id: 1,
device_id: 1,
unit: 0.022121944444444,
month: "7",
hour: "1",
minute: "0",
created_at: "2016-07-18 00:00:00",
updated_at: "2016-07-18 00:00:00"
},
{
p_id: 2,
device_id: 1,
unit: 0.022121944444444,
month: "7",
hour: "1",
minute: "5",
created_at: "2016-07-18 00:00:00",
updated_at: "2016-07-18 00:00:00"
}
]
But I want to run this Query
Device::with('units')->groupBY('hour')
->selectRaw('round(sum(unit),4) as yAxis,hour as xAxis')
->where('user_id',2)->get();
}
And this query generate error like
QueryException in Connection.php line 713:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'unit' in 'field list' (SQL: select round(sum(unit),4) as yAxis,hour as xAxis from `devices` where `user_id` = 2 group by `hour`)
So please help me in query.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire