模型

用hasone,hasMany主键是当前表
用belongsTo主键是关联表。
当关联ID在主表(当前模型的表)的时候用belongsTo
当关联ID在附表的时候用hasone,hasmany
class Banner extends Model
{

public function items() { //建立一对多关联
    return $this->hasMany('BannerItem', 'banner_id', 'id'); //关联的模型,外键,当前模型的主键
}


public static function getBannerByID($id)	
{
    $banner = self::with('items')->find($id); // 通过 with 使用关联模型,参数为关联关系的方法名
    return $banner;
}

}

class BannerItem extends Model
{

protected $hidden = ['delete_time', 'update_time'];
/**
 * 建立与 Image 表的关联模型(一对一)
 * @return \think\model\relation\BelongsTo
 */
public function img() {
    return $this->belongsTo('Image', 'img_id', 'id'); //关联模型名,外键名,关联模型的主键
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值