Heim  >  Fragen und Antworten  >  Hauptteil

So summieren Sie Elemente einer bestimmten Person (ID) - Laravel

<p>Hallo, wie kann ich die Tabellenspalte für eine bestimmte Person (ID) summieren? Ich habe zum Beispiel eine Tabelle wie diese (dies ist die ProjectHistory-Tabelle): </p> <p>Ich möchte alle Summen für einen bestimmten Mitarbeiter in meiner Colabs-Ansicht anzeigen, siehe zum Beispiel das Bild unten. (Dies ist die Mitarbeitertabelle)</p> <p>Wenn Sie sich also das Bild ansehen, sehe ich die ID 2 der „David“-Kollaboration. Ich möchte also die Summe der ProjectHistory-Tabelle für Colab mit der ID 2 in der Spalte „Suma“ ausgeben, also ( 3 + 500 = 503 ). Ich möchte dies für alle Kooperationen tun. Zum Beispiel möchte ich für colab_id 3 (für id 3 bedeutet Valentin) 2500+1800 = 4300 in der „Colaboratori“-Ansicht anzeigen. was soll ich machen? Bitte geben Sie mir eine Idee</p>
P粉450079266P粉450079266382 Tage vor364

Antworte allen(1)Ich werde antworten

  • P粉790187507

    P粉7901875072023-09-05 10:53:19

    You can use the sum in the QueryBuilder.

    像这样:

    $data = ProjectHistory::where('id', 1)->sum('suma');

    In your case, you may also use the withSum to obtain the result you want. Like so:

    $data = Collaborator::withSum('ProjectHistory', 'suma')->get();

    This resulting Collaborator will have an extra key now named: projecthistory_sum_suma which will have the sum of all the suma belonging to that Collaborator.

    This of course assumes that you have a hasMany relation with the ProjectHistory model.

    Antwort
    0
  • StornierenAntwort