php - Get reply which having most vote in laravel -


i have reply relationship & unlike model. user can best reply , unlike bad reply. & unlike relationship same,just store in different table. want replies having & reply having unlike, compare them , show replies have number of vote. how can achieve it?

in discussion model

public function replies(){     return $this->hasmany('app\forum\reply'); } 

in reply model

public function discussion(){     return $this->belongsto('app\forum\discussion'); }  public function user(){     return $this->belongsto('app\user'); }  public function likes(){     return $this->hasmany('app\forum\like'); } 

in model

public function user(){     return $this->belongsto('app\user'); }  public function reply(){     return $this->belongsto('app\forum\reply'); } 

i think can use eloquents withcount here.

so you'd have like:

$mostlikedreply = reply::withcount('likes')->orderby('likes_count', 'desc')->first(); $mostunlikedreply = reply::withcount('unlikes')->orderby('unlikes_count', 'desc')->first(); 

note withcount place {relation}_count column on resulting models. that's why orderby ordering on withcount applies, grabbing first result, should highest liked/unliked reply, here can compare need.

read more counting relations here


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -

What is happening when Matlab is starting a "parallel pool"? -