doushan5245 2016-06-25 06:10
浏览 1070

Laravel Eloquent使用“with”条件

I have two tables, say Products and Biddings where one product can be bid by many users. Naturally I have two models:

class Product extends Model
{
    public function biddings()
    {
        return $this->hasMany('App\Bidding');
    }
}

class Bidding extends Model
{
    public function product()
    {
        return $this->belongsTo('App\Product');
    }
}

So, say I want to get all products along with the highest priced bidding I did something like this.

$productBidding = DB::table('biddings')
                 ->select('*', DB::raw('max(price) as price'))
                 ->join('products', 'products.id', '=', 'biddings.product_id')
                 ->groupBy('product_id')
                 ->get();

That works well BUT I kinda want to do it Eloquent way. So how do I convert Query Builder way to Eloquent? I am currently on this but do not know how to put the "max" condition in.

$productBidding = Products::with('biddings')
            ->get();
  • 写回答

2条回答 默认 最新

  • doucheng7234 2016-06-25 07:56
    关注
    $productbinding=Bidding:with('product')
                  ->get();
    
    foreach($productbinding as $productbind) 
    {
       echo $productbind->product->name;  // example
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)