简体   繁体   English

laravel如何获取多个计数查询?

[英]how do i get multiple count queries with laravel?

I need some query builder to get result like this : 我需要一些查询生成器来获得如下结果:

select 
A.`idalbum`, A.`title_en` as `title`, 
A.`desc_en` as `description`, A.`cover` as `cover`,
(SELECT COUNT(idgallery) AS gambar FROM gallery WHERE cate='1' AND albumid=A.idalbum) as gambar,
(SELECT COUNT(idgallery) AS videos FROM gallery WHERE cate='2' AND albumid=A.idalbum) as videos,
from `album` as A

what queries i should use? 我应该使用什么查询? in case, i can't use RAW because i need some paginate on laravel Eloquent 以防万一,我不能使用RAW,因为我需要在laravel Eloquent上进行一些分页

i got this answer from another response : 我从另一个回应中得到了这个答案:

$result = DB::table('album as A')
        ->select("A.idalbum", "A.title_en as title", 
        "A.desc_en as description", "A.cover as cover",
        DB::raw("(SELECT COUNT(idgallery) AS gambar FROM gallery WHERE cate='1' AND albumid=A.idalbum) as gambar"), 
        DB::raw("(SELECT COUNT(idgallery) AS videos FROM gallery WHERE cate='2' AND albumid=A.idalbum) as videos")
        )
        ->orderBy('A.date', 'asc')->paginate($this->perpage);


 return $result;

Should i delete this question? 我应该删除这个问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM