//進行關聯定義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那個文章裏面寫了