Skip and take all?

耗尽温柔 提交于 2019-12-10 14:44:48

问题


In eloquent, how can I skip 10 rows and then get the rest of the table?

User::skip(10)->all();

The above does not work, but it gives you an idea what I am looking for.


回答1:


Try this:

$count = User::count();
$skip = 10;

User::skip($skip)->take($count - $skip)->get();

With one query:

User::skip($skip)->take(18446744073709551615)->get();

It's ugly, but it's an example from official MySQL manual:

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;



回答2:


try something like this it work for sure..

$temp = User::count();
$count = $temp - 10;

$data = User::take($count)->skip(10)->get();



回答3:


Laravel 5 returns Eloquent result as Collection. So you can use collenction function slice();

$users = User::get();
$slicedUsers = $users->slice(10);


来源:https://stackoverflow.com/questions/38915099/skip-and-take-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!