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);
  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动