duanfan1869
2016-01-05 09:29
浏览 27
已采纳

如何按照雄辩的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);

图片转代码服务由CSDN问答提供 功能建议

我的拍卖有很多竞拍者。 现在我想订购每次拍卖的投标人数量的拍卖。 我怎样才能做到这一点 ?

型号:Auction.php

 公共函数投标人()
 {
返回$ this-> hasMany('App  \ Bidder','FK_auction_id'); 
} 
   
 
 

我试过这样,但这显然是错误的,但它给了我一个想法 我试图完成。 控制器:

  $ auctions =拍卖:: orderBy($ auction-> bidders-> count(),'desc') - > paginate(9); \  n   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmi1872 2016-01-05 09:54
    已采纳

    Try something like that...

    Auction::select('auctions.*, COUNT(bids.id) as bids_count')
    ->leftjoin('bids', 'bids.FK_auction_id', '=', 'auctions.id')
    ->groupBy('bids.id')->orderBy('bids_count', 'desc')->paginate(9);
    
    点赞 打赏 评论
  • douzhan5262 2016-01-05 09:55

    I have also found this answer :

    $auctions = Auction::leftJoin('bidders','auctions.id','=','bidders.FK_auction_id')->
                   selectRaw('auctions.*, count(bidders.FK_auction_id) AS `count`')->
                   groupBy('auctions.id')->
                   orderBy('count','DESC')->
                   paginate(9);
    
    点赞 打赏 评论

相关推荐 更多相似问题