Laravel雄辩的限制和偏移 [英] Laravel Eloquent limit and offset
本文介绍了Laravel雄辩的限制和偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的
$art = Article::where('id',$article)->firstOrFail();
$products = $art->products;
我只想拿一个极限产品" 这是错误的方式
I just wanna take a limit 'product' This is wrong way
$products = $art->products->offset($offset*$limit)->take($limit)->get();
请帮帮我!
谢谢!
推荐答案
skip = OFFSET
$products = $art->products->skip(0)->take(10)->get(); //get first 10 rows
$products = $art->products->skip(10)->take(10)->get(); //get next 10 rows
来自laravel doc 5.2 https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset
From laravel doc 5.2 https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset
跳过/获取
要限制查询返回的结果数,或跳过 给定查询中的结果数(OFFSET),则可以使用跳过 并采取方法:
To limit the number of results returned from the query, or to skip a given number of results in the query (OFFSET), you may use the skip and take methods:
$users = DB::table('users')->skip(10)->take(5)->get();
在 laravel 5.3 中,您可以编写( 查看全文