周大胖子 发表于 2019-7-4 15:58:57

Laravel 多条件、模糊搜索 + 分页

首先,确认前提,由于我的数据表是生成的,所以只能用DB门面,无模型类使用 :
    /*
      获取商标数据
    */
    public function shops( Request $req )
    {
      // 页码
      $page = Input::get('page');

      // 条数
      $limit = Input::get('limit');

      $key = input::post('key') ;

      $map = array();

      // 关键词
      if(!empty(input::post('key')))
      {

            $map = function($query) use($key){
                $query->where( 'spfw', 'like', '%' . $key . '%');
                $query->orWhere( 'xtype', '=',$key );
                $query->orWhere( 'mtype', '=',$key );
                $query->orWhere( 'ctype', '=',$key );
            };

      }

      // 获取表名
      $xlist = $this->getxlist();

      // 分页检查
      $req_data = DB::table($xlist)->where( $map )->paginate($limit);

      // 统计总数
      $req_conut = DB::table($xlist)->where( $map )->count();

      // dd(collect( $req_data) ->get('data') );

      downjson(0,"信息获取成功",$req_conut,collect( $req_data ) ->get('data'));


    }


1. [分页] 这个 paginate 会自动获取参数中的 page值 【无聊不,难不成自以为是方便?那干嘛不再获取一个limit】
2. [处理返回的数据 ] 返回的数据 是个Collection 的类 , 需要经过特殊处理【 这是为了方便模板语法使用吧 】 。处理这个类 需要引入类
use Illuminate\Support\Collection;3.我整整捣鼓了好一会 才找到这么一行数字 处理方法 collect( $req_data ) ->get('data') 说实话我真不知道这是干嘛的。反正得到我想要的数据了。
    参考文献: https://laravelacademy.org/post/8210.html[其实并不能找到对应的方法,不过可以了解 这个对象]

4. [模糊搜索] $map['a'] = ['like',11]; (是错的) ,TP5这套在这很明显是行不通的。 整个度娘都在说 模型类 ,最后小文给我找了一篇文章讲DB的
参考文献 https://blog.csdn.net/sqc157400661/article/details/73850306[两种模糊查询都有]

页: [1]
查看完整版本: Laravel 多条件、模糊搜索 + 分页