语法:hasMany('关联模型','当前模型外键','当前模型主键'),例如:hasMany(Comments::class, 'article_id', 'id')
<?php
namespace app\model;
use think\Model;
class Article extends Model
{
public function comments()
{
/**
* hasMany('关联模型', '关联模型外键','当前模型主键');
* Comments 评论模型
* article_id 评论表的外键字段,关联模型外键
* id 文章表主键字段,当前模型主键
*/
return $this->hasMany(Comments::class, 'article_id', 'id');
}
}
结果: