0

I a, new learner and working on home project. I stuck to to get additions of 2 array values . The array values are as follows, Thanks for your help.. Thanks u

Array 1

array:1 [▼
    0 => array:3 [▼
        "totalbmilk" => "168.00"
        "totala2milk" => "0.00"
        "totaljmilk" => "390.00"
    ]
]

Array 2

array:1 [▼
    0 => array:3 [▼
        "totalbmilk" => "8.00"
        "totala2milk" => "5.50"
        "totaljmilk" => "2.50"
    ]
]

Expecting

  • totalbmilk = 168 + 8 = 176
  • totala2milk = 0 + 5.5 = 5.50
  • totaljmilk = 390 + 2.50 = 392.50

Controller file

$milksalefordairy = Dairymilksale::selectraw('
        SUM(buffalomilk) as "totalbmilk",
        SUM(a2milk) as "totala2milk",
        SUM(jerseymilk) as "totaljmilk"
    ')
    ->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
    ->get()
    ->toArray();
    
$milksaleforcustomer = Customermilksale::selectraw('
        SUM(buffalomilk) as "totalbmilk", 
        SUM(a2milk) as "totala2milk",
        SUM(jerseymilk) as "totaljmilk"
    ')
    ->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
    ->get()
    ->toArray();

2 Answers 2

1

You could use collection methods.

$milksalefordairy = Dairymilksale::selectraw('
        SUM(buffalomilk) as "totalbmilk",
        SUM(a2milk) as "totala2milk",
        SUM(jerseymilk) as "totaljmilk"
    ')
    ->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
    ->get()
    ->toArray();
    
$milksaleforcustomer = Customermilksale::selectraw('
        SUM(buffalomilk) as "totalbmilk", 
        SUM(a2milk) as "totala2milk",
        SUM(jerseymilk) as "totaljmilk"
    ')
    ->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
    ->get()
    ->toArray();

$collection = collect([...$milksalefordairy, ...$milksaleforcustomer]);

echo $collection->sum('totalbmilk');
echo $collection->sum('totala2milk');
echo $collection->sum('totaljmilk');
Sign up to request clarification or add additional context in comments.

Comments

0

you can use collection laravel, work in charm :)

 $array = [
    [
        "express_fee"=> 0,
        "provider_template_id"=> 501,
        "provider_id"=> 263
    ],
    [
        "express_fee"=> 2,
        "provider_template_id"=> 220,
        "provider_id"=> 4
    ],
    [
        "express_fee"=> 40000,
        "provider_template_id"=> 220,
        "provider_id"=> 4
    ]
];

return collect($array)->groupBy('provider_template_id')->map(function($item , $key){
    return [
        'provider_templae_id' => $key, 
        'express_fee' => collect($item)->sum('express_fee')
    ];
})->values();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.