Laravel Controller get limit number item

ぃ、小莉子 提交于 2019-12-10 19:18:28

问题


In my Controller

$items = Item::all();

I want to get the first 6 items only

How to change the code?


回答1:


Use take() method:

$items = Item::take(6)->get();



回答2:


Use take function

 $items = Item::take(6)->get();

Check in laravel docs : https://laravel.com/docs/5.2/queries




回答3:


To limit the number of results returned from the query you may use the take() method.

$items = Item::take(6)->get();



回答4:


or this

$items = Item::paginate(6);


来源:https://stackoverflow.com/questions/38930388/laravel-controller-get-limit-number-item

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