In my Controller
$items = Item::all();
I want to get the first 6 items only
How to change the code?
In my Controller
$items = Item::all();
I want to get the first 6 items only
How to change the code?
Use take
function
$items = Item::take(6)->get();
Check in laravel docs : https://laravel.com/docs/5.2/queries
To limit the number of results returned from the query you may use the take()
method.
$items = Item::take(6)->get();
Simply use limit()
and pass 'number of records' to limit method.
$items = Item::limit(6)->get();
Alternatively, you can use take()
as well.
$items = Item::take(6)->get();
Use methods which are supported by your Laravel Version. Hope it helps!