Search code examples
laraveleloquentlaravel-relations

Laravel count values from collection


How to get the sum of the multiplication of this data.

Here is my kvit table

id | hamkor_id | oper_type 
1  |  10       |     20     

Here is operation table

id | kvit_id | product_id | store_id| amount | price 
1  |     1   |     5      |    1    |    10  |   15
2  |     1   |     6      |    1    |    5   |   10

Here is relationships

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

Here is controller

$user = Auth::id();
    $datas = Kvit::with('user', 'hamkor', 'store', 'operation')->where('user_id', $user)->get();
    return view('operations.index', compact('datas'));

Here is my view

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

I need to get the total amount by multiplying the amount per product by the price. That is the total amount of the receipt Like this (1015 + 510) = 200 How can it possible?


Solution

  • If I understand correctly, you want to calculate the sum of the amount * price column in the Operation table, grouped by kvit_id?

    You can solve this by adding a custom attribute to your Kvit model.

    // Kvit model
    class Kvit extends Model
    {
        public function getTotalPriceAttribute(): float
        {
            return $this->operation->sum( function($operation) {
                return $operation->amount * $operation->price;
            });
        }
    }
    

    In your Blade view you can simply call:

    {{ $data->kvit->total_price }}
    

    This is one way to solve your question.

    A couple of remarks:

    1. Shouldn't operation be pluralized since it is a HasMany relation? (operations)?
    2. I have used float as return type, this could however be an integer and depends on your implementation.
    3. You could remove $idx => from your foreach. Instead, you could use the Loop variable and call {{ $loop->iteration }} instead of {{ $idx + 1 }}

    Full example:

    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