Laravel Eloquent ORM 多条件查询的例子

一、需求:

在数据搜索时最常见的就是调用同一个方法查询,而查询的字段却可能是其中一个或其中的几个字段一起组合查询,例如:对列表的搜索,基本上都是几个字段随意组合搜索。那么在model里就需要判断有那个字段组合,怎么组合。

网上找了很久,Laravel群里也问了几个,都说没有写过,于是自己写个吧。话不多说,见代码:

function findByParam($param = array())
 {
  $select = new Customer();
  if (isset($param['name']) && '' != $param['name'])
  {
   $select = $select->where('customer.name', '=', $param['name']);
  }
  if (isset($param['phone']) && '' != $param['phone'])
  {
   $select = $select->where('customer.phone', '=', $param['phone']);
  }
  if (isset($param['email']) && '' != $param['email'])
  {
   $select = $select->where('customer.email', '=', $param['email']);
  }
  if (isset($param['tel']) && '' != $param['tel'])
  {
   $select = $select->where('customer.tel', '=', $param['tel']);
  }
  if (isset($param['qq']) && '' != $param['qq'])
  {
   $select = $select->where('customer.qq', '=', $param['qq']);
  }
  if (isset($param['IDCard']) && '' != $param['IDCard'])
  {
   $select = $select->where('customer.IDCard', '=', $param['IDCard']);
  } 

  $customers = $select->leftJoin("member", function ($join)
  {
   $join->on("customer.memberID", "=", "member.id");
  })
   ->get(array(
   'customer.id',
   'customer.name',
   'customer.sex',
   'customer.tel',
   'customer.phone',
   'customer.address',
   'customer.email',
   'customer.qq',
   'customer.headPic',
   'customer.birthday',
   'customer.IDCard',
   'customer.enable',
   'customer.memberID',
   'customer.IDCard',
   'customer.info',
   'member.name as mname',
   'member.discount'
  ));
  return json_encode($customers); 

调用的时候,controller里只需要接收这些字段,无论它是否有值,直接加入到$param数组中查询就OK,例如:

function anyFindbyparam()
 {
  $name = Input::get('name');
  $tel = Input::get('tel');
  $phone = Input::get('phone');
  $email = Input::get('email');
  $qq = Input::get('qq');
  $IDCard = Input::get('IDCard');
  $customer = new Customer();
  $customers = $customer->findByParam(array(
   'name' => $name,
   'tel' => $tel,
   'phone' => $phone,
   'email' => $email,
   'qq' => $qq,
   'IDCard' => $IDCard
  ));
  return $customers;
 } 

以上这篇Laravel Eloquent ORM 多条件查询的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Laravel实现ORM带条件搜索分页

    Laravel条件搜索一般使用where方法,如下: 查询构造器: $users = DB::table('users')->where('votes', '=', 100)->get(); 或者ORM: $users = User::where('votes', '=', 100)->all(); 当有多个条件时,可以多次调用where方法: $articles = Article::where('id','>','10')->where('is_auth','=','1'

  • Laravel ORM 数据model操作教程

    随机查询 $data=Move::where('release',1) ->where('is_hot',1) ->where('is_status',1) ->orderBy(\DB::raw('RAND()')) ->take(4) ->get(); 1.ORM操作需要创建对应的model class User extends Eloquent 2.有两种方式使用数据操作对象 a. 使用new关键字创建对象后执行对象的方法 b. 直接调用static方法(实际并发静态方法

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

    筛选分页每页的条数: <select class="form-control" id="perPage" name="perPage"> @foreach ( [10,20,30,50] as $e) <option value="{{$e}}" {{ $e==request('perPage') ? 'selected' : '' }} >{{$e}}</option> @endforea

  • laravel orm 关联条件查询代码

    如下所示: public function a() { return $this->belongsTo('App\Models\a', 'aid'); } $model = $this->whereHas('a', function ($query) use ($search) { $query->where('username', 'like', '%' . $search['username'] . '%'); })->with(['a:id,username'])->g

  • Laravel Eloquent ORM 实现查询表中指定的字段

    在使用Laravel ORM的Model方法find, get, first方法获取数据对象时返回的数据对象的attributes属性数组里会包含数据表中所有的字段对应的键值关系, 那么如何在ORM查询时只返回数据表中指定字段的数据呢?很多时候,文档上没有写明的用法需要我们去看源码来探究的,下面我们就来看一下这三个方法的实现. 由于ORM依赖了QueryBuilder来实现查询, 在QueryBuilder的源码里通过查看get,first方法的实现可以到,他们都可以接收一个数组参数来指定要查询

  • Laravel Eloquent ORM 多条件查询的例子

    一.需求: 在数据搜索时最常见的就是调用同一个方法查询,而查询的字段却可能是其中一个或其中的几个字段一起组合查询,例如:对列表的搜索,基本上都是几个字段随意组合搜索.那么在model里就需要判断有那个字段组合,怎么组合. 网上找了很久,Laravel群里也问了几个,都说没有写过,于是自己写个吧.话不多说,见代码: function findByParam($param = array()) { $select = new Customer(); if (isset($param['name'])

  • 解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题

    问题描述:在使用laravel的左联接查询的时候遇到一个问题,查询中带了右表一个筛选条件,导致结果没有返回右表为空的记录. 先附上代码: DB::table('users as u') ->select('u.user_id','c.class') ->leftJoin('class as c','c.user_id','=','u.user_id') ->where('c.status','=',2) ->get(); 解决方案: 1.在mysql的角度上说,直接加where条件

  • Laravel关系模型指定条件查询方法

    对于关系模型来说,有时候我们需要甄别关联后结果,例如,班级和学生是一对多关联,我现在查询班级,但是想只显示正常状态,即状态为1的学生,因为有的学生从这个班级里面删除了,状态是4,那么我们在查询的时候就可以使用如下语法: 1.定义关联关系: Class模型: public function learners() { return $this->belongsToMany('App\Models\Customer', 'learner_relation', 'class_id', 'learner_

  • 一个简单实现多条件查询的例子

    在我们的网站设计过程中,经常会用到多条件查询,本文的源码是一个二手房屋查询的例子.在本例中,我们要实现能够通过地理位置,物业类型,房屋价格,房屋面积及信息发布日期等多个条件查询到客户所需的资料.以下是实现过程. 查询条件界面(略): 查询文件(search.php) 一.生成查询语句: <? $conn=mysql_connect("localhost","root",""); $db=mysql_select_db("lingy

  • laravel框架数据库操作、查询构建器、Eloquent ORM操作实例分析

    本文实例讲述了laravel框架数据库操作.查询构建器.Eloquent ORM操作.分享给大家供大家参考,具体如下: 1.连接数据库 laravel连接数据库的配置文件位于config/database.php中,在其中connection字段中包含laravel所支持的数据库的配置信息,可以看到其中有主机.端口.数据库.用户名.密码等信息: 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'),

  • Laravel框架Eloquent ORM简介、模型建立及查询数据操作详解

    本文实例讲述了Laravel框架Eloquent ORM简介.模型建立及查询数据操作.分享给大家供大家参考,具体如下: 注:以下知识点可能有不全面之处,望见谅 NO.1Eloquent ORM简介 Laravel所自带的Eloquent ORM是一个优美.简洁的ActiveRecord实现,用来实现数据库操作 每个数据表都有与之相对应的"模型(Model)"用于和数据交互 NO.2模型的建立 最基础的模型代码如下: namespace App; use Illuminate\Datab

  • laravel多条件查询方法(and,or嵌套查询)

    说明 在日常开发中,经常会需要写多条件的数据库查询语句.在使用框架的情况下,单纯使用原生sql查询会导致结果与model无法对应,也就没有办法使用框架的一些便利的方法对结果集进行处理.尤其是laravel提供了非常多的对查询结果集进行处理的工具.所以最好是使用laravel提供的ORM进行多条件的数据库查询. 问题 比如需要执行这样一条sql语句 select * from homework where (id between 1 and 10 or id between 50 and 70)

随机推荐