A query needs to be "worked out", or put another way, I'd probably write it using regular sql, get it working and then convert to orm. Just my thought. But look at this query that was "worked out" https://laracasts.com/discuss/channels/eloquent/orderby-computed-related-attribute
As sometimes the first attempt may not work.
Also wrap code in 3 backtics "```" no apostrophes. Also see https://help.github.com/categories/writing-on-github/