2015-11-02 34 views
1

我怎樣才能在此查詢最新的8票通過手機號碼字段:獲得最新的8條laravel 5.1

results = DB::table('votes')->whereIn('votes.id', function($query){ 
        $query->select(DB::raw('MAX(id) as id')) 
        ->from('votes') 
        ->groupBy('mobile_number', 'position_id','code'); 
       })->where(array('code'=>$code,'position_id'=>$positionId))->get(); 

此查詢它獲取用戶的最新總票數但不限於:8 。

->where(array('code'=>$code,'position_id'=>$positionId))->take(8)->get(); //it seems this is not working 

這裏的樣品臺

id mobile_number code position_id 
1 123123   1  1 
2 123123   3  1 
3 321212   2  2 
4 123123   4  1 
5 123123   5  1 
6 123123   6  1 
7 123123   7  1 
8 123123   8  1 
9 123123   9  1 
10 123123   10  1 

,如果我得到的123123票,將只能得到最新8等上表中的Id 1將不會包含在結果中。

有什麼想法?由於

+0

如果我是你,我會看口才它將使查詢非常容易:-) – Anoxy

回答

0

試試這個

對於年長版本

take(8)->skip(8)->get(); 

對於版本

limit(8)->offset(8)->get(); 
+0

其沒有得到結果 – Cris

+0

不能測量? –

+0

試着用這個'orderBy(?Query?) - > take(5) - > get()' –