3

In my Controller

$items = Item::all();

I want to get the first 6 items only

How to change the code?

Walker Base
  • 177
  • 4
  • 15

5 Answers5

8

Use take() method:

$items = Item::take(6)->get();
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

Use take function

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

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

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

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

$items = Item::take(6)->get();
Amanullah Aman
  • 633
  • 1
  • 12
  • 29
0

or this

$items = Item::paginate(6);
A. Apola
  • 131
  • 1
  • 4
  • 13
0

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!

Shweta
  • 661
  • 6
  • 11