先上代码吧:public function img()
{
return $this->hasMany('ProductImage', 'product_id', 'id');
}
public function property()
{
return $this->hasMany('ProductProperty', 'product_id', 'id');
}
public static function lstProducts()
{
return self::with(['img.imgUrl', 'property'])->select(); //想在这一步实现分页
}
用的是TP5的框架,这个是一个产品(Product)类的Model,类中有的img和property方法是关联模型的方法。
我想在利用tp的with关联模型查询的时候实现分页,确实尝试了很多方法都不行,当我渲染页面{$data->render()}的时候就报render方法不存在。有没有谁遇到过这种情况啊?
手册上的方法也试过了,都不行,救救孩子吧。
{
return $this->hasMany('ProductImage', 'product_id', 'id');
}
public function property()
{
return $this->hasMany('ProductProperty', 'product_id', 'id');
}
public static function lstProducts()
{
return self::with(['img.imgUrl', 'property'])->select(); //想在这一步实现分页
}
用的是TP5的框架,这个是一个产品(Product)类的Model,类中有的img和property方法是关联模型的方法。
我想在利用tp的with关联模型查询的时候实现分页,确实尝试了很多方法都不行,当我渲染页面{$data->render()}的时候就报render方法不存在。有没有谁遇到过这种情况啊?
手册上的方法也试过了,都不行,救救孩子吧。
->where('sh_status',0)
->where(function (Query $query) use ($param){
$keyword = empty($param['keyword']) ? '' : $param['keyword'];
if (!empty($keyword)) {
$query->where('shop_name|legal_person|phone|address', 'like', "%$keyword%");
}
})
->order("create_time DESC")->paginate(10);
// 分页显示
$lists->each(function ($item,$key){
$servicearr = explode(',',$item['service']);
$service = Db::name('category')->where('id','in',$servicearr)->field('name')->select()->toArray();
$item['service'] = $service;
$region = new RegionModel();
$province = $region->field('name')->find($item['province']);
$item['province'] = $province;
$city = $region->field('name')->find($item['city']);
$item['city'] = $city;
$county = $region->field('name')->find($item['county']);
$item['county'] = $county;
$street = $region->field('name')->find($item['street']);
$item['street'] = $street;
});
模型中:public static function lstProducts()
{
return self::with(['img.imgUrl', 'property', 'category.img'])->paginate(10);
}
控制器中:public function lst()
{
$result = ProductModel::lstProducts();
$page = $result->render();
$this->assign(['products' => $result, 'page' => $page]);
return $this->fetch();
}
在控制器中再调用一次render方法赋值给$page,同时渲染$page内容视图中:<div style="padding-top: 10px" ; width:230px>
{$page}
</div>
这样就可以实现with关联模型的分页了,手册上的链式操作在这里不适用了。