laravel where中多条件查询

 

1、

http://www.mobanstore.com/doc/bianchengkaifa/119.html

//初学laravel 发现他的查询构造器很好用
//如下
$user = DB::table('users')->where('name', 'John')->first();
var_dump($user->name); //但是 如果where里面有多个查询条件怎么办呢? 我查了查源码 原来是可以用数组的,这样就灵活多了 public static function article_data($id) {   //$id = Input::get('id');   $where = array('a.id' => $id, 'a.rm'=>0); // 关键是这里   $data = DB::table('article AS a')     //->where('a.id', '=', $id)     ->where($where) ->leftJoin('category AS b', 'a.catid', '=', 'b.id') ->select('a.*', 'b.name AS catname', 'b.id AS catid' ) ->get();   if($data)   {     return $data[0];   }   //$sql = DB::getQueryLog();   //var_export( $sql);die; }

 

2、

http://www.cnblogs.com/yjf512/p/4031782.html

//解决办法:在基类中扩展一个multiwhere 
//于是我就在BaseModel中定义了:

    // 多where
    public function scopeMultiwhere($query, $arr)
    {
        if (!is_array($arr)) {
            return $query;
        }
 
        foreach ($arr as $key => $value) {
            $query = $query->where($key, $value);
        }
        return $query;
    }

 

//这样子,上面的语句就可以这么使用:
Student::multiwhere([‘female’=>1, ’teacher_id’ => 4, ‘class_id’ => 3])->get();

 

posted @ 2015-11-01 16:21  忘忧般若汤  阅读(11670)  评论(0编辑  收藏  举报