如何计算关系集合中的数据jenssegers laravel mongodb
情况1:
$photos = Photo::with(['like', 'comment' => function ($query) {
$query->where('is_approved', '1');}])
->select('photo_featured', 'title', 'view_count')->where('status', "1")->orderBy('created_at', 'desc')
->get();
视图:
$ photo-> like-> count()
$ photo->评论-> count()
但是它会选择喜欢和评论收集中的所有数据,然后进行计数 ->消耗内存,处理器速度慢
情况2:与withCount一起使用
->返回:非法的偏移量类型
https://github.com/jenssegers/laravel-mongodb/issues/1339
https://github.com/jenssegers/laravel-mongodb/issues/1147
https://github.com/jenssegers/laravel-mongodb/issues/1416
我没有找到解决错误的方法
情况3:使用查询生成器和原始
$photos = DB::table('photos')
->select(['photos.photo_featured', 'photos.title', 'photos.view_count', DB::raw("COUNT(photos_like._id) as like_count"),
DB::raw("COUNT(photos_comment._id) as comment_count")])
->....
->返回:非法的偏移量类型
使用DB :: raw
时出错
https://github.com/jenssegers/laravel-mongodb/issues/1123
https://github.com/jenssegers/laravel-mongodb/issues/1693
如何从表关系中获取计数?