//进行关联定义belongsTo设置(一对一关系) public function img(){ //关联模型名,外键名,当前模型主键名 return $this->belongsTo("ImageModel","img_id","id"); }
//进行关联定义hasMany设置(一对多关系) public function items(){ //关联模型名,外键名,当前模型主键名 return $this->hasMany("BannerItemModel","banner_id","id"); }
public static function getBannerByID($id){ //使用模型自带的方法进行数据获取 $banners = self::where("id",$id)->with(["items","items.img"])->select(); return $banners; }
//定义多对多的关系,一个专题可以有多个产品,一个产品可以归于多个专题 public function products(){ //关联模型,中间表,关联表主键在中间表中的表示,主表主键在中间表中的表示 return $this->belongsToMany("ProductModel","theme_product","product_id","theme_id"); }
1对1对1三表关联可以看fastadmin那个文章里面写了