This query:
$ids = EnergyMeters::leftJoin('meteringpoint_energymeter_relation', 'energymeter.id', '=',
'meteringpoint_energymeter_relation.meterId')
->where('id', '=', $meterPointId)
->where('Deleted', '=', '0')
->select('energymeter.id')
->orderBy('name')
->get();
gives me some ids as a result (structured as key, val)
[{"id":"03cd0c39-a51c-41a0-bbe3-37ae641d051e"},{"id":"0ebf61e7-b751-4931-b737-d8f71297e499"}, {"id"}, '...']
However I only need the ids as an array for this new sum-query:
Data::select(array(DB::RAW('sum(v1) as sumV1', 'sum(v2) as sumV2')))
->whereIn('id', $ids)
->where('PointOfTime', '>', $from)
->where('PointOfTime', '<=', $to + 86400)
->get();
Any eloquent solutions for this? Otherwise I would loop over the key and val array and push they value to a new array. Not that smart?
// edited the question to make it more clear
I believe there is a function (its just not in the official docs)
I found this in the source of Laravel:
/**
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @return array
*/
public function lists($column, $key = null)
{
// ... lots of code ...
}
So try ->lists('id');
On the first query results set use;
$data = Data::select(array(DB::RAW('sum(v1) as sumV1', 'sum(v2) as sumV2')))
->whereIn('id', $ids)
->where('PointOfTime', '>', $from)
->where('PointOfTime', '<=', $to + 86400)
->get();
$ids = $data->lists('id');
Notice the lists('id')
this will give you the array you want. Then you can pass the results into the other query.
https://laravel.com/docs/5.6/queries#retrieving-results says:
Retrieving A List Of Column Values If you would like to retrieve a Collection containing the values of a single column, you may use the pluck method. In this example, we'll retrieve a Collection of role titles:
$titles = DB::table('roles')->pluck('title');
foreach ($titles as $title) {
echo $title;
}