DB::table('table')->orderBy(DB::raw('RAND()'))->take(10)->get();
Jun 28, 2015
3
Level 36
selecting a random number of entries
i want to select a limited number of random entries from the database.
with pure sql pretty much a no-brainer:
select * from table
order by rand()
limit(10)
using the query builder it doesn't work. rand() is not being accepted:
DB::table('table')
->orderBy(rand())
->limit(10)
using orderByRaw doesn't work either (at least not the ways i tried it)
Any ideas how i get a limited random number of entries out of the table?
Level 41
1 like
Please or to participate in this conversation.