0

I have a query about this project that I am doing, I make a query to my two tables and the data that I call in this case is a quantity number for both, the data displayed is the one that has the same id for both tables. enter image description here

The problem occurs when I pass two identifiers and to those two identifiers I want to add their current amount with the amount obtained from the other table

In general, what I want to do is add the amounts obtained, this is my code that I am working with, I would really appreciate if you can help me solve it or guide me.

  $id_servis = [1077,1078];
    $sum_quantity_add = Servis_tareas::where('servis_id',$id_servis)->get();
    foreach($sum_quantity_add as $sum_add){
        $quantity_two[] = $sum_add->quantity;
    }
    $quantity_actual = Servis::wherein('id',$id_servis)->get();
    foreach($quantity_actual as $quantity_act){
       $quantity_one[] = $quantity_act->quantity_final;
    }
    dd($id_servicios,$quantity_one, $quantity_two);
     //ERROR 
      $total[] = $quantity_one + $quantity_two;
     //ERROR 
     if(is_numeric($total) < 0 ){
        Servis::wherein('id',$id_servis)->update(['quantity_final' => 0]);
     }else{
        Servis::wherein('id',$id_servis)->update(['quantity_final' => $total]);
     }
JL DIAZ
  • 93
  • 7

1 Answers1

1

In MySql/SQL there is SUM query which handles the addition and they are called Aggregation Functions, and in Laravel there is a Eloquent equivalent of these Laravel Aggregates, using these methods you will be able to count, max, min, avg on the query end rather than in the PHP end.

So, your code will look like

        $id_servis        = [1077, 1078];
        $sum_quantity_add = Servis_tareas::where('servis_id', $id_servis)->SUM('quantity');
        $quantity_actual  = Servis::wherein('id', $id_servis)->SUM('quantity_final');
        $total            = $sum_quantity_add + $quantity_actual;

What you are trying is treating array as numeric value and adding it, which is wrong, + operator behaves totally different while you are using with array, it merges the two array, it is different than array_merge too, so i recommend giving this answer a read + operator for array in PHP

UPDATED:

I still don't understand if you want to replace with the SUM from Servis_tareas in the Servis Table or sum the each others quantity and save it, Code below sum the data from both table and save it.

$id_servis    = [1077, 1078];
$servisTareas = Servis_tareas::selectRaw("SUM(`quantity`) as total, `servis_id` ")
    ->where('servis_id', $id_servis)
    ->groupBy('servis_id')
    ->having('total', '>', 0)
    ->get();

$foundId     = [];
$servisTotal = Servis::query()->whereIn('id', $id_servis)->pluck('quantity_final', 'id')->toArray();
foreach ($servisTareas as $servisTarea) {
    $foundId[] = $servisTarea->servis_id;
    $total     = $servisTarea->total + ($servisTotal[$servisTarea->servis_id] ?? 0)
    Servis::where('id', $servisTarea->servis_id)->update(['quantity' => $total]);
}
if (!empty($foundId)) {
    Servis::whereNotIn('id', $foundId)->update(['quantity' => 0]);
}
Anuj Shrestha
  • 966
  • 6
  • 18
  • Thank you very much for your help, but what the code does is add the total and what I want to do is add the new quantity of my "Servis_tareas" model to that of "Servis", when I pass only one ID if it works for me it adds the quantity current plus the new one, but the problem starts when I give it two identifiers, what I want to do is All the Servis that have the same ID of Servis_Tareas add their respective quantity as I show in the image, I want the quantity of the third array go to those of the second array that is to say that the 0 becomes 4 and also the other 0 becomes 2 – JL DIAZ Nov 21 '22 at 15:41
  • 1
    @JLDIAZ, updated, check it out – Anuj Shrestha Nov 22 '22 at 08:57
  • Thank you very much, with your help I can do what I needed, just modify when doing the update, add the square brackets to make it work for me like this Servis::where('id', [$servisTarea->servis_id])->update(['quantity' => $total]); – JL DIAZ Nov 22 '22 at 22:40
  • @JLDIAZ Just saw that my last query was mistake it should have been `whereNotIn` – Anuj Shrestha Nov 23 '22 at 05:01