douyong6585 2014-11-07 14:08
浏览 89
已采纳

检索数组而不是key:val(laravel / eloquent)

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

  • 写回答

3条回答 默认 最新

  • douji9734 2014-11-07 14:26
    关注

    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');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效