如何通过Laravel Eloquent执行这个复杂的查询?

时间:2014-12-13 12:31:16

标签: php laravel-4

我在SQL中有这个复杂的查询:

SELECT distinct(albumId) as id,
band,albumName as name,
SUM(numberOfPlays) as numberOfPlays
FROM (
SELECT COUNT(*) as numberOfPlays,
tracks.album_id as albumId,
a.name as albumName,b.name as band
FROM logs l
LEFT JOIN tracks ON tracks.id=l.track_id
LEFT JOIN albums a ON tracks.album_id = a.id
LEFT JOIN bands b ON b.id = tracks.band_id
WHERE l.time>$days
GROUP by trackid
ORDER BY numberOfPlays DESC LIMIT 0,100) t
GROUP BY albumId ORDER BY numberOfPlays DESC");

到目前为止,我已成功使用以下方法检索内部查询:

Logs::select(DB::raw(
                    'COUNT(*) as numberOfPlays'
                ), 'track_id')
               ->where('time', '>', $days)
               ->orderBy('numberOfPlays', 'DESC')->take(100)
               ->with('track', 'track.album')
               ->groupBy(\DB::raw("track_id"))->orderBy('numberOfPlays', 'DESC');

如何对上述查询返回的数据进行进一步查询?

1 个答案:

答案 0 :(得分:1)

您可以通过Eloquent ORM实际获得类似的结果,并使用返回的Collection结果中的可用方法。这是一个例子。考虑到你有所有的模型类。

$logs = Log::with(['track.album', 'track.band'])->where('time', '>', $days)->take(100)get();




class Log extends Eloquent {

   public function track() {
      return $this->belongsTo('Track');
   }
}


Class Track extends Eloquent {

   public function album() {
     return $this->belongsTo('Album');
   }

   public function band() {
     return $this->belongsTo('Band');
   }

}

Class Album extends Eloquent {

}


Class Band extends Eloquent {}

现在以您想要的方式生成结果,您可以使用返回的集合将其项目转换为可呈现的结构。

首先我们将按照专辑

对日志进行分组
$logsByAlbum = $logs->groupBy('track.album.id');

然后转换分组日志

$logsByAlbum->transform(function($album_logs) {

    if(!empty($album_logs)) {

      return array(
        'id' => $album_logs[0]->album->id,
        'album_name' => $album_logs[0]->album->name,
        'numberOfPlays' => count($album_logs)
      );

    }

    return array();

});

After transformation, we need to filter the items with empty values
$final_result = $logsByAlbum->filter(function($item) { 
      return !empty($item);
});

return $final_result->toArray();

这只是帮助您使用Eloquent ORM以及集合类中提供的方法的强大功能的基本思想。

享受! :d