laravel 关联关系 自定义分页(还是采用与laravel自带的样式相同)
来源:互联网 发布:js url编码解码 编辑:程序博客网 时间:2024/06/06 08:32
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:
<?phpnamespace 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>
阅读全文
0 0
- laravel 关联关系 自定义分页(还是采用与laravel自带的样式相同)
- Laravel自定义分页样式
- Laravel自定义分页样式
- laravel自定义分页样式
- Laravel自定义分页样式
- laravel 自带分页与 groupBy共用
- laravel的Eloquent关联关系
- Laravel 更改分页样式
- laravel自定义分页
- laravel框架自带的邮件类
- laravel框架总结 -- 关联关系
- laravel 关联关系遍历数组
- Laravel中关联关系总结
- laravel与分页
- laravel中分页带参数
- laravel 自带消息notification
- laravel原生分页并带参数的搜索分页
- laravel的分页
- 使用unordered_map的时候,出现C2338: The C++ Standard doesn't provide a hash for this type.
- 突破SafeSEH机制之三——利用加载模块之外的地址绕过SafeSEH
- C++异常机制,一篇文章让你了解异常
- linux 查耗费资源的进程
- 【JZOJ5233】【GDOI模拟8.5】概率博弈
- laravel 关联关系 自定义分页(还是采用与laravel自带的样式相同)
- 重构笔记——入门篇
- oracle使用rownum进行分页语句详解
- J.U.C下的并发容器的并发实现
- openssl RSA 内存读取密钥
- 使用AndroidAutoLayout遇到的一些问题
- ansible初接触
- [leetcode]116. Populating Next Right Pointers in Each Node@Java解题报告
- 条理清晰的搭建SSH环境之整合Hibernate和Spring