belongsTomany 是多对多的关系 表数据是多对多的情况下使用

hasMany是一对多的关系

belongsToMany('关联模型名','中间表名','外键名','当前模型关联键
名',['模型别名定义'])

public function products()
    {
        return $this->belongsToMany('Product','theme_product',
            'product_id','theme_id');
    }

tp5 belongsToMany和hasMany场景

 

 

hasMany('关联模型名','外键名','主键名',['模型别名定义']);

例如一篇文章可以有多个评论

<?php
namespace app\index\model;
use think\Model;
class Article extends Model
{
public function comments()
{
return $this->hasMany('Comment','art_id');
}
}

相关文章:

  • 2022-01-08
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
猜你喜欢
  • 2021-11-07
  • 2021-12-12
  • 2022-12-23
  • 2021-09-04
  • 2021-12-10
  • 2021-12-23
  • 2021-07-01
相关资源
相似解决方案