Laravel Eloquent 限制和偏移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35643192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent limit and offset
提问by Sang Tr?n
This is mine
这是我的
$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();
Please give me a hand!
请帮我一把!
Thanks!
谢谢!
回答by Atiqur
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
From laravel doc 5.2 https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset
来自 laravel doc 5.2 https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset
skip / take
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();
跳过/采取
要限制查询返回的结果数量,或跳过查询中给定数量的结果(OFFSET),您可以使用 skip 和 take 方法:
$users = DB::table('users')->skip(10)->take(5)->get();
In laravel 5.3you can write (https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset)
在laravel 5.3 中你可以写(https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset)
$products = $art->products->offset(0)->limit(10)->get();
回答by water_ak47
Quick:
快的:
Laravel has a fast pagination method, paginate, which only needs to pass in the number of data displayed per page.
Laravel 有一个快速的分页方法 paginate,它只需要传入每页显示的数据数量。
//use the paginate
Book::orderBy('updated_at', 'desc')->paginate(8);
how to customize paging:
如何自定义分页:
you can use these method: offset
,limit
,skip
,take
你可以使用这些方法:offset
,limit
,skip
,take
offset,limit : where does the offset setting start, limiting the amount of data to be queried
skip,take: skip skips a few pieces of data and takes a lot of data
offset,limit : offset设置从哪里开始,限制要查询的数据量
skip,take:skip 跳过几条数据,取很多数据
for example:
例如:
Model::offset(0)->limit(10)->get();
Model::skip(3)->take(3)->get();
//i use it in my project, work fine ~
class BookController extends Controller
{
public function getList(Request $request) {
$page = $request->has('page') ? $request->get('page') : 1;
$limit = $request->has('limit') ? $request->get('limit') : 10;
$books = Book::where('status', 0)->limit($limit)->offset(($page - 1) * $limit)->get()->toArray();
return $this->getResponse($books, count($books));
}
}
回答by nageen nayak
laravel have own function skip
for offset
and take
for limit
. just like below example of laravel query :-
laravel 有自己的功能skip
foroffset
和take
for limit
。就像下面的laravel查询示例一样:-
Article::where([['user_id','=',auth()->user()->id]])
->where([['title','LIKE',"%".$text_val."%"]])
->orderBy('id','DESC')
->skip(0)
->take(2)
->get();
回答by Ali
You can use skip
and take
functions as below:
您可以使用skip
和take
功能如下:
$products = $art->products->skip($offset*$limit)->take($limit)->get();
// skip
should be passed param as integer value to skip the records and starting index
//skip
应将参数作为整数值传递以跳过记录和起始索引
// take
gets an integer value to get the no. of records after starting index defined by skip
//take
获取一个整数值以获取编号。由定义的开始索引后的记录数skip
EDIT
编辑
Sorry. I was misunderstood with your question. If you want something like pagination the forPage
method will work for you. forPage method works for collections.
对不起。我对你的问题有误解。如果你想要分页之类的东西,这个forPage
方法对你有用。forPage 方法适用于集合。
REf : https://laravel.com/docs/5.1/collections#method-forpage
参考:https://laravel.com/docs/5.1/collections#method-forpage
e.g
例如
$products = $art->products->forPage($page,$limit);
回答by Salem Megiddo
Maybe this
也许这
$products = $art->products->take($limit)->skip($offset)->get();
$products = $art->products->take($limit)->skip($offset)->get();
回答by Byron Jester Manalo
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);
$chunk = $collection->forPage(2, 3);
$chunk->all();
回答by Pramodya Abeysinghe
Try this sample code:
试试这个示例代码:
$art = Article::where('id',$article)->firstOrFail();
$products = $art->products->take($limit);
回答by Nisam
Please try like this,
请尝试这样,
return Article::where('id',$article)->with(['products'=>function($products, $offset, $limit) {
return $products->offset($offset*$limit)->limit($limit);
}])