【发布时间】:2021-10-20 05:21:36
【问题描述】:
我目前正在制作一个新闻系统,我需要获得点赞最多的前 5 个帖子。在我的代码和表格下方:
型号:
帖子:
class Post extends Model
{
use HasFactory;
use HasTags;
public function user()
{
return $this->belongsTo(User::class);
}
public function likes()
{
return $this->hasMany(PostLike::class);
}
public function comments()
{
return $this->hasMany(PostComment::class);
}
}
喜欢:
class PostLike extends Model
{
protected $fillable = ['user_id', 'post_id', 'is_liked'];
public function user(){
return $this->belongsTo(User::class);
}
public function post(){
return $this->belongsTo(Post::class);
}
use HasFactory;
}
表格:
帖子: posts table
post_likes: post likes table
【问题讨论】: