Saturday, April 1, 2017

Laravel orderBy using 2 tables?

Please don't tell me to put the orderBy inside the function, because that doesn't work. kill_count and death_count are columns of the roleplay relationship

here is my full code:

$topCredits = Player::whereHas("roleplay", function($q) {
            $q->where('government_id', '<', '1');
        })->orderBy('credits', 'DESC')->limit(10)->get();

        $topKills = Player::whereHas("roleplay", function($q) {
            $q->where('government_id', '<', '1')->orderBy('kill_count', 'DESC');
        })->limit(10)->get();

        $topDeaths = Player::whereHas("roleplay", function($q) {
            $q->where('government_id', '<', '1')->orderBy('death_count', 'DESC');
        })->limit(10)->get();

Now, the first $topCredits works perfectly, but the bottom 2 don't. They work very well except for the fact it doesn't order by the death_count and kill_count, can anyone help with this? The roleplay is a relationship of hasOne on both sides.



via VoiD HD

Advertisement