【发布时间】:2021-09-19 03:03:52
【问题描述】:
如何得到这个数据相乘的总和。
这是我的 kvit 表
id | hamkor_id | oper_type
1 | 10 | 20
这是操作表
id | kvit_id | product_id | store_id| amount | price
1 | 1 | 5 | 1 | 10 | 15
2 | 1 | 6 | 1 | 5 | 10
这里是关系
class Kvit extends Model
{
use HasFactory;
public function operation(){
return $this->hasMany(Operation::class);
}
public function hamkor(){
return $this->belongsTo(User::class, 'hamkor_id','id');
}
public function user(){
return $this->belongsTo(User::class);
}
public function store(){
return $this->belongsTo(Store::class);
}
}
这里是控制器
$user = Auth::id();
$datas = Kvit::with('user', 'hamkor', 'store', 'operation')->where('user_id', $user)->get();
return view('operations.index', compact('datas'));
这是我的看法
<table class="datatables-basic table" id="example">
<thead>
<tr>
<th>#</th>
<th></th>
<th>Date</th>
<th>Hamkor</th>
<th>Store</th>
<th>Summ</th>
<th>Amallar</th>
</tr>
</thead>
<tbody>
@foreach($datas as $idx => $data)
<tr>
<td>{{$idx+1}}</td>
<td></td>
<td>{{$data->date}}</td>
<td>{{$data->hamkor->name}}</td>
<td>{{$data->store->name}}</td>
<td>{{ **here i want to get result(200)** }}</td>
<td>
<a href="#" class="btn btn-icon btn-flat-primary">
<i class="fas fa-edit"></i>
Edit
</a>
<a href="#" class="btn btn-icon btn-flat-primary">
<i class="fas fa-view"></i>
View
</a>
<a href="#" class="btn btn-icon btn-flat-primary">
<i class="fas fa-trash"></i>
Delete
</a>
</td>
</tr>
@endforeach
</tbody>
我需要通过将每个产品的数量乘以价格来获得总金额。 那就是收据的总金额 像这样 (1015 + 510) = 200 怎么可能?
【问题讨论】:
-
我不完全理解你的问题。也许您可以在原始问题中指定应将哪些模型和哪些列相乘?
-
这里是公式
sum (amount * price) group by kvit_id = 1 -
我已经添加了一个答案,希望它能解决你的问题。
标签: laravel eloquent laravel-relations