K
K
knowledge2018-05-20 19:28:24
Laravel
knowledge, 2018-05-20 19:28:24

Output sorted records in related models in laravel?

I have a vote, three models VHeader - the topic of the vote
VOption - answer options and VUser - these are actually which users vote for which options
They are all connected hierarchically as one to many
And when the user voted, I need to display the VOption in accordance with the number of votes
if naked sql, then something like this

select voption.title, count(vuser.voption_id) as cnt from option
left join vuser on vuser.voption_id = voption.id
where voption.vheader.id = :id
groupBy voption.id
orderBy cnt DESC

how to implement it in eloquent?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question