【问题标题】:Laravel count values from collectionLaravel 计算集合中的值
【发布时间】: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


【解决方案1】:

如果我理解正确,您要计算Operation 表中amount * price 列的sum,按kvit_id 分组?

您可以通过将custom attribute 添加到您的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. 您可以从您的foreach 中删除$idx =&gt;。相反,您可以使用Loop variable 并调用{{ $loop-&gt;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

【讨论】:

    【解决方案2】:

    这不是测试它,我现在没有任何例子,但也许你可以解决这样的问题。在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>
    

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 2019-12-25
      • 1970-01-01
      • 2022-01-18
      • 2021-01-30
      • 2016-03-26
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多