mercredi 30 novembre 2016

Use Laravel Collection to get duplicate values

I don´t remove this duplicate values, I want get the articles_id duplicates and sum hers quantities values, por example, this is my collection:

Collection {#306 ▼
  #items: array:3 [▼
    0 => CartLine {#294 ▼
      +quantity: 2
      +article_id: 1728
      +article_name: "TAZA CERAMICA"
    }
    1 => CartLine {#296 ▼
      +parent_line_id: null
      +quantity: 1
      +article_id: 1728
      +article_name: "TAZA CERAMICA"
    }
    2 => CartLine {#298 ▼
      +quantity: 1
      +article_id: 1378
      +article_name: "JARRA CERVEZA ALEMANA"

    }
  ]
}

And I want get this result:

Collection {#306 ▼
  #items: array:3 [▼
    0 => CartLine {#294 ▼
      +quantity: 3 //sum total quantity of the duplicates elements with same article_id
      +article_id: 1728
      +article_name: "TAZA CERAMICA"
    }
    1 => CartLine {#296 ▼
      +parent_line_id: null
      +quantity: 3
      +article_id: 1728
      +article_name: "TAZA CERAMICA"
    }
    2 => CartLine {#298 ▼
      +quantity: 1
      +article_id: 1378
      +article_name: "JARRA CERVEZA ALEMANA"

    }
  ]
}

I want sum the quantities of this duplicate elements and set the quantity property with the sum only in this elements.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire