1,laravel自带的分页:
\vendor\laravel\framework\src\Illuminate\Pagination\LengthAwarePaginator.php
运用这个方法:
/**
* Create a new paginator instance.
*
* @param mixed $items
* @param int $total
* @param int $perPage
* @param int|null $currentPage
* @param array $options (path路径, query执行, fragment, pageName)
* @return void
*/
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
{
foreach ($options as $key => $value) {
$this->{$key} = $value;
}
$this->total = $total;
$this->perPage = $perPage;
$this->lastPage = (int) ceil($total / $perPage);
$this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
$this->items = $items instanceof Collection ? $items : Collection::make($items);
}
2,我的controller
public function index(Request $request) {
$search=$request->input('search');
$page = $request->has('page')?$request->input('page'):1;
$paginate = 15;
//搜索功能
if($request->has('search')){
$search = $request->input('search');
$count = Goods::where('name', 'like', '%' . $search . '%')->count();
}else{
$count = Goods::count();//总数
}
//计算一页显示几条数据
$total_page = ceil($count / $paginate);
$temp = [];
for($i=0;$i<$count;$i=$i+$paginate){
$temp[] = $i;
}
if($request->has('search')){
$search=$request->input('search');
$goods = Goods::where('name', 'like', '%' . $search . '%')
->offset($temp[$page - 1])
->limit($paginate)->get();
}else{
$goods = Goods::offset($temp[$page - 1])->limit($paginate)->get();
}
//laravel的关联关系,注意modal
$data = [];
foreach($goods as $key=>$v){
//dump($v->goodsclass);
$data[$key]['id'] = $v->id;
$data[$key]['name'] = $v->name;
$data[$key]['product_code'] = $v->product_code;
$data[$key]['class_name'] = $v->goodsclass->class_name;
$data[$key]['nums'] = $v->goodsNexus->count();
$data[$key]['price'] = $v->price;
}
//dd($data);
//传参数给\vendor\laravel\framework\src\Illuminate\Pagination\LengthAwarePaginator.php
//要use Illuminate\Pagination\LengthAwarePaginator;
$goods = new LengthAwarePaginator($data, $count, $paginate, $page, ['path' => '/admin/commodity']);
$goodsClasses = GoodsClass::where('fid', 0)->get();
return view("admin.commodity",[
'goodsClasses'=>$goodsClasses,
'goods'=>$goods,
'search'=>$search,
'total_page'=>$total_page
]);
}
3,modal:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Goods extends Model
{
protected $table='11';
protected $guarded = [];
public function 11(){
return $this->hasMany('App\Models\11', 'id');
}
public function 11(){
return $this->belongsTo('App\Models\111','id');
}
}
public function 11() { return $this->belongsTo('App\Models\11', 'id'); } public function hasChildren(){ return self::where('fid',$this->id)->exists(); } public function childrens(){ return self::where('fid',$this->id)->get(); } public function 11() { return $this->belongsTo('App\Models\11','id'); }
4,view@foreach($goods as $good) <tr> <td>{{$good['id']}} </td></tr>