2014-12-13 35 views
6

我注意到,在Laravel鏈接skip()時,您還必須使用take()。我想跳過前n行,但休息一下。 take方法只允許整數,我怎樣才能做到這一點,而不訴諸於一些hacky的技巧,如指定一個大數目的服用?Laravel雄辯地跳過n,全部拿走?

回答

14

基本上,每一個OFFSET,都必須提供一個LIMIT來工作。因此,沒有確定限制就無法做到這一點。我們需要一些PHP mojo在這裏工作。

假設我們有一個名爲Attendance的Eloquent類。下面是應該工作:

//Getting count 
$count = Attendance::count(); 
$skip = 5; 
$limit = $count - $skip; // the limit 
$collection = Attendance::skip($skip)->take($limit)->get(); 
+0

我同意。有趣的閱​​讀也http://stackoverflow.com/questions/255517/mysql-offset-infinite-rows – jhmilan 2014-12-13 18:05:02

2

我認爲這不是一個很好的答案,因爲你強迫做兩個查詢,以正確的方式將是:

$collection = Attendance::skip($skip)->take($limit)->get(); 
$collection.shift(); 

你可以看到更多的集合here

+0

這個答案更正確 – Oldenborg 2016-06-23 10:47:42

+0

你能解釋你的代碼嗎? – 2018-02-28 04:58:49