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