mardi 5 janvier 2016

how to order by count of related items in eloquent laravel 5.1?

I have Auctions who have many bidders. Now I want to order the auctions on the amount of bidders each auction has. How can I do this ?

model : Auction.php

public function bidders()
{
    return $this->hasMany('App\Bidder', 'FK_auction_id');
}

I tried it like this , but that is obviously wrong , but it gives an idea of what i'm trying to accomplish. Controller:

$auctions = Auction::orderBy($auction->bidders->count() , 'desc')->paginate(9);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire