2017-04-11 65 views
0

我有兩個表,一個用於評論,另一個用於喜歡,每行在喜歡錶中,評論表中共享相同的評論ID,以便我可以將它們鏈接在一起。不過,我只是通過foreach與他們聯繫。使用兩個表獲取最喜歡的評論

@foreach ($getNotesAll as $getnotes) 

<?php 
$upvoteCount = $getNoteVotes->where('noteUniqueID', $getnotes->noteUnique)- 
>where('like-type', 'upvote')->count(); 
?> 

@endforeach 

使用

$getNoteVotes = noteLikes::all()->where('type', 'upvote'); 
    $getNotesAll = Notes::orderBy('created_at','asc')->paginate(10); 

我在一個點,我需要得到最喜歡的意見,所以我得查詢如表要求最重複的評論的ID行按降序排列,然後使用類似查詢中的評論ID查詢評論表。我不能完全讓我解決這個頭不使用foreach循環,但我認爲這是使用模型實現,但任何指導,將不勝感激

回答

0

嘗試是這樣的:

DB::table('commentsTable') 
    ->join('likesTable', 'commentsTable.id', '=', 'likesTable.comment_id') 
    ->select('commentsTable.tittle',DB::raw("count(likesTable.comment_id) as count")) 
    ->groupBy('likesTable.comment_id') 
    ->orderBy('likesTable.comment_id','asc') 
    ->get(); 
+0

謝謝回答,什麼是commentsTable.tittle指的是什麼? –

+0

我不知道你的數據庫結構,所以我做了,但是一個評論已經有一個標題..只需替換你想傳遞給視圖的屬性 – user2963176