Laravel 4.1: Eloquent Offset & Limit

2020-06-11 13:40发布

How to limit returned data from Eloquent? I tried with this:

$data = Product::all()->take(4)->skip(3);

And it return the error message: Call to undefined method Illuminate\Database\Eloquent\Collection::skip()

It seems like eloquent don't support skip()? So, how can I offset & limit the data from eloquent?

Thank you.

2条回答
Summer. ? 凉城
2楼-- · 2020-06-11 14:12

laravel have own function skip for offset and take for limit. just like below example of laravel query :-

Product::where([['title','=',$text_val]])
                ->skip(0)
                ->take(2) //get first 2 rows
                ->get();
查看更多
叛逆
3楼-- · 2020-06-11 14:19

You may try this (get 4 items from offset 3/4th):

Product::take(4)->offset(3)->get();

Or this (get 5 items from 3rd row):

Product::take(5)->skip(2)->get();
查看更多
登录 后发表回答