I’m doing a project and is very limited in time; now there’s just no time to sit and study the SQL translation in Eloquent, especially since the SQL query is not trivial:
SELECT user_id FROM user_movies GROUP BY user_id ORDER BY COUNT( movie_id IN (SELECT movie_id FROM user_movies WHERE user_id = 305344) ) DESC LIMIT 50 But the matter is not even in the request itself, the question is simpler:
How to use SQL queries as they are, but in the Laravel controller?
If interested, the query does the following: searches all records in the general table (each record has a user id and a movie id repeatedly found in the table), groups them by user id, then sorts the number of identical films in descending order compared to the current user, id which - 305344. To translate this into Eloquent, my week will be gone.
ORDER BY COUNT(логическое выражение)is on the verge of genius ... was it not easier to take 2 copies of the table at once, related by movie_id equality and NOT user_id equality? And also - formally, the query idea implicitly assumes that the user, whose id = 305344, will be the first to be issued, but if there is a user who has ALL movies that a given user has, then it is unknown who will be the first in the final set .. - AkinaORDER BY user_id = 305344 DESC, остальные выражения(for MySQL, or similarly through CASE for other dialects) - then you can guarantee the required order. Here you only have to pass the parameter twice - to the condition of selection and to the condition of sorting, but this is hardly a problem. - AkinaAND user_id != 305344not it easier to stupidly in WHERE addAND user_id != 305344? before sorting it will not enter the set ... - Akina