laravel 关联关系 自定义分页(还是采用与laravel自带的样式相同)

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>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值