Using Eloquent subqueries to randomise grouped results

Grouping items in a database query and then selecting a random item from each group is surprisingly tricky. As with most things involving GROUP BY , it doesn’t work quite how you 1 might expect. Let’s dig in. The contrived scenario Imagine you’re building a movie recommendation app. The films are stored in a movies table, which looks like this: title genre The Exorcist Horror The Raid Action The…