comment 文章表 comment_id 主键 id 文章表关联主键
index/model/Article.php
<?php
namespace app\index\model;
use think\Model;
class Article extends Model
{
public function comment()
{
return $this->hasMany('comment','id','id');
}
}
?>index/model/Comment.php<?php
namespace app\index\model;
use think\Model;
class Comment extends Model
{
public function article()
{
return $this->belongsTo('article');
}
}
?>控制器内代码use app\index\model\Article;
//查询所有每篇文章下的评论内容且分页
$article_list = Article::with('comment')->order('add_time DESC')->field('id,title,add_time,introduction,clicknum')->paginate(20); 最佳答案