【问题标题】:Get Count in Eloquent Relationship在雄辩的关系中获得计数
【发布时间】:2020-10-18 13:04:15
【问题描述】:

我想在 'likes' 表中获取相同 article_id 的计数

article.php 模型

public function like()
{
    return $this->belongsTo('App\Models\Like', 'id', 'article_id');
}

目前这是我的查询

    $mentor = $this->article->with("articlecategory")->with("user")->with(['like' => function ($query) use ($qUserId) {
        $query->where("user_id", $qUserId);
    }])->orderby("id", "DESC")->get();

IT 返回如下内容:

{
   "id":66,
   "user_id":317,
   "cat_id":4,
   "title":"a",
   "photo":"",
   "content":"a",
   "created_at":"2020-05-29 19:01:12",
   "updated_at":"2020-05-29 19:01:12",
   "articlecategory":{
      "id":4,
      "title":"Financial",
      "created_at":null,
      "updated_at":null
   },
   "user":{
      "id":317,
      "state_id":null,
      "city_id":null,
      "category_id":10,
      "sub_cat_id":0,
      "name":"jeff2",
      "mname":null,
      "lname":"benzos2",
      "gender":null,
      "email":"jeff@gmail.com",
      "phone":"11111111111",
      "telephone":null,
      "role":"Mentor",
      "salaryrange":null,
      "ccode":null,
      "fbmsg":null,
      "skype":null,
      "viber":null,
      "activate":1,
      "about":"222a\naasd\n\na",
      "fbmsg_user":null,
      "skype_user":null,
      "viber_user":null,
      "expr":null,
      "skill":null,
      "education":null,
      "cover":"",
      "bdate":null,
      "facebookurl":null,
      "twitterurl":null,
      "linkedurl":null,
      "googleurl":null,
      "youtubeurl":null,
      "pnp":null,
      "pnpfile":"0",
      "sss":null,
      "sssfile":"0",
      "barangay":null,
      "nbi":null,
      "tin":"0",
      "last_lat":null,
      "last_long":null,
      "last_locality":"",
      "created_at":"2020-03-23 11:59:23",
      "updated_at":"2020-06-16 06:11:00"
   },
   "like":{
      "id":4,
      "user_id":177,
      "article_id":66,
      "like_count":1,
      "created_at":"2020-06-28 14:41:10",
      "updated_at":"2020-06-28 15:59:44"
   }
}

我想要获得“articleID”的总点赞数

我可以再次调用表格“喜欢”来计算计数吗?但是怎么做?我迷路了。

我想在 'like' 数组之后添加另一个数组:

"like":{
          "id":4,
          "user_id":177,
          "article_id":66,
          "like_count":1,
          "created_at":"2020-06-28 14:41:10",
          "updated_at":"2020-06-28 15:59:44"
       },
"total_article_likes":{
        "count":"3"
}

编辑** 或者任何类型的数组,只要我能得到 article_id 中的点赞总数即可

编辑**

我收到了这个问题

 $mentor = $this->article->with("articlecategory")->with("user")->with(['like' => function ($query) use ($qUserId) {
        $query->where("user_id", $qUserId);
    }])->withCount(["like"])->orderby("id", "DESC")->get();

它可以完成这项工作,但我需要将其过滤到 like_count = 1 的位置,我该如何过滤它?

【问题讨论】:

  • like_count 是多少?不同的用户可以点赞一篇文章,那么无论使用什么用户运行查询,total_article_likes 的值都相同?如果这是真的,我认为使用 article_id 过滤的另一个查询是一个简单的解决方案。
  • 嗨,我回答了我的问题,但似乎不是我想要的,我得到了这个查询:$mentor = $this->article->with("articlecategory")->with( "user")->with(['like' => function ($query) use ($qUserId) { $query->where("user_id", $qUserId); }])->withCount(["like" ])->orderby("id", "DESC")->get();但我需要过滤更多,如何过滤?
  • 我更新了问题,需要过滤withCount,有什么办法吗?
  • 如何过滤子查询中的like_count $query->where("user_id", $qUserId)->where('like_count',1);
  • 你可以像with一样将约束添加到withCount

标签: php sql laravel eloquent


【解决方案1】:

您可以过滤像 withCount 这样的子查询 - 它会生成一个子查询 - 使用 having 子句,如下所示:

$this->article
     ->with("articlecategory")
     ->with("user")
     ->with(['like' => function ($query) use ($qUserId) {
         $query->where("user_id", $qUserId);
     }])->withCount(["like"])
     ->having('like_count','=',1) 
     ->orderby("id", "DESC")->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多