Laravel 4.1: Eloquent Offset & Limit

2020-06-11 13:34发布

问题:

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.

回答1:

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();


回答2:

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();