Tuesday, April 11, 2017

Retrieving most liked comment using two tables

I have two tables, one for comments and another for likes, each row In the likes table shares the same comment ID in the comment table so I can link them together. However I'm only relating them through a foreach.

@foreach ($getNotesAll as $getnotes)

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

@endforeach

Using

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

I'm at a point where I need to get the most liked comments, so I'll have to query the like table to request rows with the most repeating comment ID's in descending order, then querying the Comments tables with the Comment Id's from the like query. I can't quite get my head around this without using a foreach loop, though I think this is achieved using models, but any guidance would be appreciated



via jay stephens

Advertisement