关键是看懂laravel自带的 paginate() 方法。
在Builder.php文件(项目名称/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php)里面有一个分页的方法paginate()。
页面:
<div id="a" @if(isset($attr) && $attr[1] == 'spage') style="display: none" @else style="display: block" @endif > @if(empty($goods)) <div class="cell"> <div> <p>暂无商品</p> </div> </div> @else <table> @foreach($goods as $v) <div class="cell"> <img src="{{$v->picture}}" alt="暂无图片" /> <div> <p>{{$v->name}}</p> <p>所需积分:{{$v->exchange_integral}}</p> <p>库存数量:{{$v->goods_number}}</p> <div>立即兑换</div> </div> </div> <div class="zhan"></div> @endforeach </table> @endif <div class="cell" style="text-align: center" >{{$goods->appends(['spage'=>$dui->currentPage()])->links()}}<br/></div> </div> <div id="b" @if(isset($attr) && $attr[1] == 'spage') style="display: block" @else style="display: none" @endif > @if(empty($dui)) <div class="cell"> <div> <p>暂无可兑换商品</p> </div> </div> @else <table> @foreach($dui as $v) <div class="cell"> <img src="{{$v->picture}}" alt="暂无图片" /> <div> <p>{{$v->name}}</p> <p>所需积分:{{$v->exchange_integral}}</p> <p>库存数量:{{$v->goods_number}}</p> <div>立即兑换</div> </div> </div> <div class="zhan"></div> @endforeach </table> @endif <div class="cell" style="text-align: center" >{{$dui->appends(['fpage'=>$goods->currentPage()])->links()}} <br/></div> </div>
后台:
//商品列表 public function goodsList(Request $request) { //所有的商品 $goods = Goods::orderBy('updated_at','desc')->paginate(2,['*'],'fpage'); //所有的可以兑换的商品 $userid = $this->getWxUserId(); $user = User::where('wx_userid',$userid)->first(); $point= $user->point; //用户积分 $dui = Goods::where('exchange_integral','<=',$point)->orderBy('updated_at','desc')->paginate(1,['*'],'spage'); //根据键值判断当前页面 $attr = array() ; foreach($_GET as $k=>$v){ $attr[] = $k; } //首次进入,初始化 if(empty($attr)){ $attr[0] = 'spage'; $attr[1] = 'fpage'; } return view("marketing.goods.goodsList",compact('goods','dui','attr')); }
引用出处:
https://www.cnblogs.com/smallyi/p/6627855.html