我使用以下代码调用投票的所有注释的数组:
$poll = Poll::find($id);
return view('pages.poll', ['poll' => $poll, 'comments' => $poll->comments]);
comments 和民意调查之间的联系如下:
Comment.php个
public function poll() {
return $this->belongsTo(Poll::class, 'poll_id');
}
Poll.php个
public function comments() {
return $this->hasMany(Comment::class, 'poll_id');
}
最后,我想按照注释表中的列likes
对来自$poll->comment
的数组comments
进行排序,类似于DB::table('comment')->orderBy('likes')->get();
.
Is there any way to do that?个