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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?