Laravel实现搜索的时候分页并携带参数


Posted in PHP onOctober 15, 2019

筛选分页每页的条数:

<select class="form-control" id="perPage" name="perPage">
 @foreach ( [10,20,30,50] as $e)
  <option value="{{$e}}" {{ $e==request('perPage') ? 'selected' : '' }} >{{$e}}</option>
 @endforeach
</select>

路由:

Route::get('customer/index/{customer_type?}', 'CustomerController@index');

后端接口:

public function index($customer_type = null) {
  $search = request('search');
  $perPage = request('perPage') ? request('perPage') : 10;
  $customer_type = $customer_type ? $customer_type : request('customer_type');
  $data = Customer::select(['id', 'email', 'user_name', 'nick_name', 'phone', 'create_time'])
   ->where('customer_type', '=', $customer_type)
   ->where(function ($query) use ($search) {
    if ($search) {
     $query->where('user_name', 'like', '%' . $search . '%')
      ->orWhere('nick_name', 'like', '%' . $search . '%')
      ->orWhere('phone', 'like', '%' . $search . '%')
      ->orWhere('email', 'like', '%' . $search . '%');
    }
   })
   ->orderBy('create_time', 'desc')
   ->paginate($perPage);
  //追加额外参数,例如搜索条件
  $appendData = $data->appends(array(
   'search' => $search,
   'customer_type' => $customer_type,
   'perPage' => $perPage,
  ));
  return view('admin/customerList', compact('data'));
 }

##效果图:

Laravel实现搜索的时候分页并携带参数

Laravel实现搜索的时候分页并携带参数

前端完整代码:

@extends('admin.master')
@section('content')
<div class="wrapper wrapper-content animated fadeInRight">
 <div class="row">
  <div class="col-sm-12">
   <div class="ibox float-e-margins">
    <form class="form-inline" method="get" action="{{ url('/admin/customer/index',[request()->route('customer_type')])}}">
     <div class="form-group" style="margin-left: 20px">
     <label for="perPage">每页显示数:</label>
     <select class="form-control" id="perPage" name="perPage">
      @foreach ( [10,20,30,50] as $e)
      <option value="{{$e}}" {{ $e==request('perPage') ? 'selected' : '' }} >{{$e}}</option>
      @endforeach
     </select>
    </div>
    <div class="form-group" style="margin-left: 20px">
     <label for="search">模糊搜索:</label>
     <input type="text" name="search" style="width: 400px" class="form-control" id="search" placeholder="请输入机构名或者邮箱或者电话" value="{{request('search')}}">
    </div>
    <button type="submit" class="btn btn-primary" style="margin-left: 20px">开始搜索</button>
   </form>
   {{-- 表格内容 --}}
   <div class="ibox-content">
    <table class="table table-hover table-bordered table-condensed">
     <thead>
      <tr class="success">
       <th class="text-center">用户ID</th>
       <th class="text-center">用户电话</th>
       <th class="text-center">用户邮箱</th>
       <th class="text-center">用户名</th>
       <th class="text-center">用户昵称</th>
       <th class="text-center">注册时间</th>
       <th class="text-center">操作</th>
      </tr>
     </thead>
     @if ($data->total()>0)

     <tbody>
      @foreach ($data as $element)
      {{-- {{dd($element)}} --}}
      <tr class="gradeU {{ ($element['status']==4)?'bg-danger':'' }}">
       <td>{{$element->id}}</td>
       <td class="center">{{$element->phone}}</td>
       <td>{{$element->email}}</td>
       <td>{{$element->user_name}}</td>
       <td>{{$element->nick_name}}</td>
       <td>{{$element->create_time}}</td>
       <td>
        <a class="btn btn-info" href="{{ url('admin/customer/getInfo',[$element->id] )}}" rel="external nofollow" >详细</a>
        <a class="btn btn-success" href="{{ url('admin/customer/readCustomer',[$element->id] )}}" rel="external nofollow" >修改</a>
        <a class="btn btn-danger" href="{{ url('admin/customer/softDeleteCustomer',[$element->id] )}}" rel="external nofollow" >删除</a>
       </td>
      </tr>
      @endforeach
     </tbody>
    </table>
    <div class="text-center">{!! $data->render() !!}</div>
    @else
    <tbody>
     <tr ><td colspan="7"><div class="text-center"><h3>没有查到相关数据!</h3></div></td></tr>
    </tbody>
   </table>
   @endif
  </div>
 </div>
</div>
</div>
</div>
@endsection

带筛选的:

<form class="form-inline" method="get" action="{{ url('dataInfo/channel_form_data',request('id'))}}">
 <div class="form-group" style="margin-left: 20px">
  <label for="search">状态筛选:</label>
  <select name="user_status" class="form-control">
   <option>所有状态</option>
   @foreach ($user_status as $key=>$element)
   <option value="{{$key}}" {{request('user_status')==$key?'selected':''}}>{{$element}}</option>
   @endforeach
  </select>
  <label for="search">模糊搜索:</label>
  <input type="text" name="search" style="width: 400px" class="form-control" id="search" placeholder="用户名或者邮箱" value="{{request('search')}}"> 
 </div>
 <button type="submit" class="btn btn-primary" style="margin-left: 20px">开始搜索</button>
 <a href="{{url('dataInfo/create_channel_user_data',request('id'))}}" rel="external nofollow" class="btn btn-primary" style="float:right;">新增渠道用户</a>
</form>

以上这篇Laravel实现搜索的时候分页并携带参数就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
将RTF格式的文件转成HTML并在网页中显示的代码
Oct 09 PHP
基于php设计模式中单例模式的应用分析
May 15 PHP
使用PHP Socket写的POP3类
Oct 30 PHP
ThinkPHP控制器间实现相互调用的方法
Oct 31 PHP
php中ftp_chdir与ftp_cdup函数用法
Nov 18 PHP
从wamp到xampp的升级之路
Apr 08 PHP
PHP异常处理浅析
May 12 PHP
PHP开发Apache服务器配置
Jul 15 PHP
php实现网站留言板功能
Nov 04 PHP
php实现用户注册密码的crypt加密
Jun 08 PHP
Ubuntu 16.04中Laravel5.4升级到5.6的步骤
Dec 07 PHP
Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解
Apr 06 PHP
在Laravel中实现使用AJAX动态刷新部分页面
Oct 15 #PHP
Yii框架的redis命令使用方法简单示例
Oct 15 #PHP
解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题
Oct 15 #PHP
解决Laravel5.5下的toArray问题
Oct 15 #PHP
laravel通过a标签从视图向控制器实现传值
Oct 15 #PHP
laravel在中间件内生成参数并且传递到控制器中的2种姿势
Oct 15 #PHP
laravel 实现划分admin和home 模块分组
Oct 15 #PHP
You might like
PHP数据库操作面向对象的优点
2006/10/09 PHP
PHP的几个常用数字判断函数代码
2012/04/24 PHP
将word转化为swf 如同百度文库般阅读实现思路及代码
2013/08/09 PHP
php中通过curl检测页面是否被百度收录
2013/09/27 PHP
PHP调用.NET的WebService 简单实例
2015/03/27 PHP
yii2中LinkPager增加总页数和总记录数的实例
2017/08/28 PHP
用javascript获取地址栏参数
2006/12/22 Javascript
javascript转换字符串为dom对象(字符串动态创建dom)
2010/05/10 Javascript
Jquery动态进行图片缩略的原理及实现
2013/08/13 Javascript
jQuery.position()方法获取不到值的安全替换方法
2015/03/13 Javascript
JavaScript 浏览器对象模型BOM使用介绍
2015/04/13 Javascript
常用DOM整理
2015/06/16 Javascript
基于JavaScript制作霓虹灯文字 代码 特效
2015/09/01 Javascript
纯JS实现弹性导航条效果
2017/03/06 Javascript
基于JS实现网页中的选项卡(两种方法)
2017/06/16 Javascript
Bootstrap实现前端登录页面带验证码功能完整示例
2020/03/26 Javascript
vue 避免变量赋值后双向绑定的操作
2020/11/07 Javascript
Python实现list反转实例汇总
2014/11/11 Python
Python多线程编程(六):可重入锁RLock
2015/04/05 Python
Python 正则表达式入门(中级篇)
2016/12/07 Python
python进程管理工具supervisor的安装与使用教程
2017/09/05 Python
13个最常用的Python深度学习库介绍
2017/10/28 Python
python中sys.argv函数精简概括
2018/07/08 Python
python画蝴蝶曲线图的实例
2019/11/21 Python
如何用OpenCV -python3实现视频物体追踪
2019/12/04 Python
pytorch 批次遍历数据集打印数据的例子
2019/12/30 Python
Python解释器及PyCharm工具安装过程
2020/02/26 Python
Python matplotlib模块及柱状图用法解析
2020/08/10 Python
用python进行视频剪辑
2020/11/02 Python
Java文件和目录(IO)操作
2014/08/26 面试题
2014年党课学习材料
2014/05/11 职场文书
安全环保演讲稿
2014/08/28 职场文书
2014年加油站站长工作总结
2014/12/23 职场文书
优秀班组事迹材料
2014/12/24 职场文书
Python list去重且保持原顺序不变的方法
2021/04/03 Python
vue实现可以快进后退的跑马灯组件
2022/04/08 Vue.js