我想在“likes”表中获得相同文章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();
它会返回以下内容:
{
"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"
}
编辑或者任何类型的数组,只要我能得到文章中喜欢的总数
编辑
我有这个问题
$mentor = $this->article->with("articlecategory")->with("user")->with(['like' => function ($query) use ($qUserId) {
$query->where("user_id", $qUserId);
}])->withCount(["like"])->orderby("id", "DESC")->get();
它做的工作,但我需要把它过滤到像\u count=1,我如何过滤它?
1条答案
按热度按时间r1zk6ea11#
您可以对子查询进行筛选,如
withCount
-它生成一个子查询-使用having
子句,如下所示: