0

如何得到这个数据的乘法之和。

这是我的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>

我需要通过将每个产品的数量乘以价格来获得总金额。那就是收据的总金额 像这样(10 15 + 5 10) = 200 怎么可能呢?

4

2 回答 2

1

如果我理解正确,您想计算表sumamount*price列的?Operation分组kvit_id

您可以通过向模型添加自定义属性来解决此问题Kvit

// 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 }}

这是解决您的问题的一种方法。

几点说明:

  1. operation既然是HasMany关系,就不应该复数吗?( operations)?
  2. 我已用作float返回类型,但这可能是一个整数,并且取决于您的实现。
  3. 你可以$idx =>从你的foreach. 相反,您可以使用Loop 变量并调用{{ $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
于 2021-07-08T17:22:16.403 回答
0

这不是测试它,我现在没有任何例子,但也许你可以解决这样的问题。在kvit模型类中创建一个方法

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;
}

并且在刀片中

<td>{{$data->self_amount()}}</td>
于 2021-07-08T18:01:39.267 回答