Eloquent Collections: Get Random Records - 2 Examples

preview_player
Показать описание
I want to show you two examples of how to get some random data from the database and shuffle results, with both Eloquent and Collections.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

Using from() you can make a subquery from which you shuffle the results. That way you won't load any more models than you need to. This should work: As for the popular tags, the same approach works. I hope youtube doesn't mark this as spam again.

intipontt
Автор

Really like the fact that you write modern Laravel code. Most of all the packages I've been seeing lately are using legacy code and they don't upgrade it to use modern syntax. They just update so it doesn't break

fahvygaming
Автор

With the random function at the end of the popular tags, wouldn’t that still load all 40 models in memory instead of the intended 10 models? If so, I think a better solution would be to randomize from the database instead of through the collection.

KastsEcho
Автор

inRandomOrder might be a good option. 🙂

Saves memory by getting exact amount of data required.

saurabhshukla
Автор

Hello Mr. Povilas Korop, Can you make a video about "how to install laravel with docker, and how to use laravel with dockera". Thank you!

MurodjonO
Автор

Hello Mr. Povilas, how can we achieve randomization with pagination for API,
From one of your replies "So I would probably store the same dataset in user's session or somewhere, and iterate through that"
I have been beating myself over this and haven't been able to find a solution,
can you please shoot a short video explaining this, ie. randomizing get query with pagination.

Thank your for great content we get to learn new things every day with @Laravel Daily

zekgyn
Автор

Hi, first of all, thank you for your work. I have a question, what is the difference between take and limit in eloquent query?

Andicot
Автор

In my opinion, this is not a very good idea. We select records, then we filter them in the collection. This is not optimal! The load goes both to the database server and to the web server. I would keep an array with ID somewhere. And from it would make a sample of random records.
Thus, we request only the necessary records for pre-randomly selected IDs

webdev
Автор

are you not calling collection queries on excessively returned data which will inflate 43 models while only using 11? would it not be better to use a database subquery to get a random item(s) (query within query inception builder method) and only return the data you're going to use?

evilwizard
Автор

Hello dear teacher, how can i use bulma css on front and boostrap on backend with vue js 3 composition api thx

UIRETU
Автор

How did you put that one course in the loop of posts?

aashiq_hasnat
Автор

eg: you have to paginate the data by the view count and 10 items per page. let say we have 50 data and all the data that have the same view count should be randomized. how will you randomized it with the pagination?

ericvaldez
Автор

Why not randomize in the database query?

wfl-junior