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)