ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1.模型中的Banner.php ~~~ <?php namespace app\api\controller\v1; use app\api\validate\IDMustBePostiveInt; use app\api\model\Banner as BannerModel; use app\lib\exception\BannerMissException; class Banner{ //获取指定id的banner信息 public function getBanner($id) { (new IDMustBePostiveInt())->goCheck(); $banner=BannerModel::with(['items','items.img'])->find($id); if(!$banner){ throw new BannerMissException(); } return $banner; } } ~~~ 2.模型中的BannerItem.php ~~~ <?php namespace app\api\model; use think\Model; class BannerItem extends Model { public function img() { return $this->belongsTo('Image', 'img_id', 'id'); } } ~~~ 3.模型中的Image.php ~~~ <?php namespace app\api\model; use think\Model; class Image extends Model { } ~~~ 4.控制器中的Banner.php ~~~ <?php namespace app\api\controller\v1; use app\api\validate\IDMustBePostiveInt; use app\api\model\Banner as BannerModel; use app\lib\exception\BannerMissException; class Banner{ //获取指定id的banner信息 public function getBanner($id) { (new IDMustBePostiveInt())->goCheck(); $banner=BannerModel::with(['items','items.img'])->find($id); if(!$banner){ throw new BannerMissException(); } return $banner; } } ~~~ 结果 ![](https://box.kancloud.cn/1f8b96aa253afd19a39a6be531b7057d_666x765.png)