1

I have this query :

$b = Class::orderBy('created_at', 'desc')->take(50)->get();

This gets me the last 50 elements in my table . However i only want every 3 rows e.g : valu1 , value3 , value5 , value6

Is there a way to process that directly in the sql query without additional code ?

Thanks

  • Does not have to be eloquent , could be an sql query that does the same as above.

Thanks

Sam.tuver
  • 679
  • 2
  • 9
  • 19
  • 2
    May be you are looking for some thing like this https://stackoverflow.com/questions/858746/how-do-you-select-every-n-th-row-from-mysql – baig772 Sep 07 '17 at 06:11
  • What do you want actually, 1th, 4th, 7th row in the data or something else ? – Sagar Gautam Sep 07 '17 at 06:23

1 Answers1

1

If you use Laravel of latest versions:

$b = Class::orderBy('created_at', 'desc')->nth(3)->get();

More about nth()

Odin Thunder
  • 3,284
  • 2
  • 28
  • 47