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 相关文章推荐
php中模拟POST传递数据的两种方法分享
Sep 16 PHP
php通过数组实现多条件查询实现方法(字符串分割)
May 06 PHP
采用ThinkPHP中F方法实现快速缓存实例
Jun 13 PHP
destoon实现公司新闻详细页添加评论功能的方法
Jul 15 PHP
PHP中怎样防止SQL注入分析
Oct 23 PHP
php连接oracle数据库及查询数据的方法
Dec 29 PHP
php命令行(cli)下执行PHP脚本文件的相对路径的问题解决方法
May 25 PHP
PHP滚动日志的代码实现
Jun 10 PHP
Yii快速入门经典教程
Dec 28 PHP
PHP中set_include_path()函数相关用法分析
Jul 18 PHP
php微信公众号开发(4)php实现自定义关键字回复
Dec 15 PHP
基于PHP实现堆排序原理及实例详解
Jun 19 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 金额数字转换成英文
2010/05/06 PHP
PHP 只允许指定IP访问(允许*号通配符过滤IP)
2014/07/08 PHP
PHP+Mysql实现多关键字与多字段生成SQL语句的函数
2014/11/05 PHP
PHP中file_exists()判断中文文件名无效的解决方法
2014/11/12 PHP
Python中使用django form表单验证的方法
2017/01/16 PHP
PHP实现防止表单重复提交功能【基于token验证】
2018/05/24 PHP
浅析jQuery的链式调用之each函数
2010/12/03 Javascript
JS+css 图片自动缩放自适应大小
2013/08/08 Javascript
js中的this关键字详解
2013/09/25 Javascript
jQuery使用之处理页面元素用法实例
2015/01/19 Javascript
jQuery中$this和$(this)的区别介绍(一看就懂)
2015/07/06 Javascript
js实现二级菜单渐隐显示
2015/11/03 Javascript
有关jQuery中parent()和siblings()的小问题
2016/06/01 Javascript
原生javascript实现的ajax异步封装功能示例
2016/11/03 Javascript
微信小程序 image组件binderror使用例子与js中的onerror区别
2017/02/15 Javascript
bootstrap table实现双击可编辑、添加、删除行功能
2017/09/27 Javascript
webpack4+Vue搭建自己的Vue-cli项目过程分享
2018/08/29 Javascript
vue封装一个简单的div框选时间的组件的方法
2019/01/06 Javascript
python编程开发之textwrap文本样式处理技巧
2015/11/13 Python
Ruby使用eventmachine为HTTP服务器添加文件下载功能
2016/04/20 Python
Python实现的txt文件去重功能示例
2018/07/07 Python
win7下python3.6安装配置方法图文教程
2018/07/31 Python
python生成n个元素的全组合方法
2018/11/13 Python
Python实现 版本号对比功能的实例代码
2019/04/18 Python
Python提取PDF内容的方法(文本、图像、线条等)
2019/09/25 Python
dpn网络的pytorch实现方式
2020/01/14 Python
对python中return与yield的区别详解
2020/03/12 Python
Django微信小程序后台开发教程的实现
2020/06/03 Python
台湾家适得:Homeget
2019/02/11 全球购物
销售类个人求职信范文
2013/09/25 职场文书
临床医学应届生求职信
2013/11/06 职场文书
竞争性谈判邀请书
2014/02/06 职场文书
构建高效课堂实施方案
2014/03/13 职场文书
中学生操行评语
2014/04/24 职场文书
维稳工作情况汇报
2014/10/27 职场文书
详解python网络进程
2021/06/15 Python