Noticias::where('url_foto', '!=', ' ')
->where('id_tipos_noticias', 1)
->orderByRaw("RAND()")
->take(8)
->get()
There is also the random method on Collection but I think less performant.
Hello there! I'm trying to obtain random items of a table in my database, but I'm not sure how.
Right now I'm using this eloquent code
Noticias::whereRaw('url_foto != " " AND id_tipos_noticias = "1"')->latest('created_at')->limit('8')->get()
But instead of the last 8 items I need 8 random items. Hope there is a way.
Noticias::where('url_foto', '!=', ' ')
->where('id_tipos_noticias', 1)
->orderByRaw("RAND()")
->take(8)
->get()
There is also the random method on Collection but I think less performant.
Please or to participate in this conversation.