Laravel 计算集合中的值
Laravel count values from collection
如何得到这个数据相乘的和。
这是我的 kvit table
id | hamkor_id | oper_type
1 | 10 | 20
这里是操作table
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
怎么可能?
如果我没理解错的话,你想计算Operation
table中amount
*price
列的sum
,按[分组=17=]?
您可以通过向 Kvit
模型添加 custom attribute 来解决此问题。
// Kvit model
class Kvit extends Model
{
public function getTotalPriceAttribute(): float
{
return $this->operation->sum( function($operation) {
return $operation->amount * $operation->price;
});
}
}
在您的 Blade 视图中,您可以简单地调用:
{{ $data->kvit->total_price }}
这是解决您问题的一种方法。
几点说明:
-
operation
是 HasMany
关系,不应该复数吗? (operations
)?
- 我将
float
用作 return 类型,但这可能是一个整数,具体取决于您的实现。
- 您可以从
foreach
中删除 $idx =>
。相反,您可以使用 Loop variable 并调用 {{ $loop->iteration }}
而不是 {{ $idx + 1 }}
完整示例:
class Operation
{
public $amount;
public $price;
public function __construct($amount, $price)
{
$this->amount = $amount;
$this->price = $price;
}
}
class Kvit
{
public $operations;
public function __construct($operations)
{
$this->operations = $operations;
}
public function calculate(): float
{
return $this->operations->sum( function($operation) {
return $operation->amount * $operation->price;
});
}
}
$operation1 = new Operation(10, 15);
$operation2 = new Operation(5, 10);
$operations = collect([$operation1, $operation2]);
$kvit = new Kvit($operations);
$kvit->calculate(); // returns 200
这不是测试,我现在没有任何例子,但也许你可以解决类似的问题。在 kvit 模型中 class 创建方法
public function self_amount()
{
$sum = 0;
if($this->operation()->exists()) // or count($this->operation)
$operation = $this->operation->filter(function($item) use ($sum) {
$sum += $item->amount * $item->price;
});
return $sum;
}
并且在 blade 只是
<td>{{$data->self_amount()}}</td>
如何得到这个数据相乘的和。
这是我的 kvit table
id | hamkor_id | oper_type
1 | 10 | 20
这里是操作table
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 怎么可能?
如果我没理解错的话,你想计算Operation
table中amount
*price
列的sum
,按[分组=17=]?
您可以通过向 Kvit
模型添加 custom attribute 来解决此问题。
// Kvit model
class Kvit extends Model
{
public function getTotalPriceAttribute(): float
{
return $this->operation->sum( function($operation) {
return $operation->amount * $operation->price;
});
}
}
在您的 Blade 视图中,您可以简单地调用:
{{ $data->kvit->total_price }}
这是解决您问题的一种方法。
几点说明:
-
operation
是HasMany
关系,不应该复数吗? (operations
)? - 我将
float
用作 return 类型,但这可能是一个整数,具体取决于您的实现。 - 您可以从
foreach
中删除$idx =>
。相反,您可以使用 Loop variable 并调用{{ $loop->iteration }}
而不是{{ $idx + 1 }}
完整示例:
class Operation
{
public $amount;
public $price;
public function __construct($amount, $price)
{
$this->amount = $amount;
$this->price = $price;
}
}
class Kvit
{
public $operations;
public function __construct($operations)
{
$this->operations = $operations;
}
public function calculate(): float
{
return $this->operations->sum( function($operation) {
return $operation->amount * $operation->price;
});
}
}
$operation1 = new Operation(10, 15);
$operation2 = new Operation(5, 10);
$operations = collect([$operation1, $operation2]);
$kvit = new Kvit($operations);
$kvit->calculate(); // returns 200
这不是测试,我现在没有任何例子,但也许你可以解决类似的问题。在 kvit 模型中 class 创建方法
public function self_amount()
{
$sum = 0;
if($this->operation()->exists()) // or count($this->operation)
$operation = $this->operation->filter(function($item) use ($sum) {
$sum += $item->amount * $item->price;
});
return $sum;
}
并且在 blade 只是
<td>{{$data->self_amount()}}</td>