public function getFollower(Request $request){
$userId = $request->get('user_id');
$artists = DB::table('artists')
->select('artists.id', 'artists.title as name ','artists.src as poster','followers.user_id',DB::raw('COUNT(followers.user_id) as followers'))
->leftjoin('followers', 'artists.id','=', 'followers.artist_id')
->where('artists.status',0)
->groupBy('followers.artist_id')
->orderBy('followers.id', 'desc')
->get();
return Response()->json(['data' => $artists], 200);
}
我想结果如下
data: [
{
"id": 3,
"name ": "Artist 3",
"poster": "",
"user_id": 1,
"followers": 1
},
{
"id": 2,
"name ": "Hello Artist 2",
"poster": "",
"user_id": 1,
"followers": 1
},
{
"id": 1,
"name ": "Thai",
"poster": "",
"user_id": 3,
"followers": 10
}
]
如果我使用jQuery的laravel - >在哪里( 'followers.user_id',$用户id)的结果将得到followers: 1
只有
任何人都可以帮助我,我怎样才能得到结果如上,我可以使用->where('followers.user_id',$userId)
谢谢!
当我登录我想要得到艺术家,我跟着他们和每个艺术家显示计数总追随者 –
如果我使用此func - > where('followers.user_id',1)我无法获得追随者结果数只有1为什么? –
为什么不使用关系而不是硬编码呢? – Demonyowh