gpt4 book ai didi

php - Laravel 所属查询

转载 作者:行者123 更新时间:2023-12-04 18:05:20 24 4
gpt4 key购买 nike

抱歉,我无法用简单的问题来描述我的问题!所以我要把它写下来以便更好地解释。

我有 2 个表..

订单表:

id - status  
100 - success
200 - pending

销售表:

id - order_id - user_id - amount 
1 - 100 - 5 - $20
2 - 200 - 5 - $30

销售模式

class Sales extends Eloquent
{
public function parentorder()
{
return $this->belongsTo('Order', 'order_id');
}
}

我只需要获得成功订单的总金额

// get profits 
// this gives me = $50
$profits = Sales::where('user_id',Auth::user()->id)->sum('amount')

// trying to do something like ..
$profits = Sales::where('user_id',Auth::user()->id)->where('parentorder.status','Success')->sum('amount');
// so I get the correct total which is $20 (for the success order id=100)

嗯,有什么帮助吗?

最佳答案

查询

$total = Sale::whereHas('orders', function($q)
{
$q->where('status', '=', 'success');

})->sum('amount');

用法

调用变量$total

关于php - Laravel 所属查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27976767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com