Laravel 4.1: Eloquent Offset & Limit

天涯浪子 提交于 2020-01-01 04:32:06

问题


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


来源:https://stackoverflow.com/questions/21923320/laravel-4-1-eloquent-offset-limit

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