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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM