mardi 11 septembre 2018

Get the highest value from a collection

I have a collection of products i.e.

Collection {#567 ▼ 
#items: array:3 [▼
0 => Product {#571 ▼
  #searchable: array:1 [▶]
  #fillable: array:17 [▶]
  #hidden: []
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:19 [▼
    "id" => 12
    "brand_id" => null
    "sku" => "1002796"
    "name" => "In quo vero error dolorem est."
    "slug" => "in-quo-vero-error-dolorem-est"
    "description" => "<p>Dicta ipsum quis soluta pariatur iure rerum quo. Voluptatibus nulla eveniet ab esse vero. Atque et quod fuga non.</p>"
    "cover" => "products/W2INR8joy2tLuRLOXv4h5r51I7Vpcjq3hXemzvzI.png"
    "quantity" => 10
    "price" => "5.00"
    "sale_price" => "10.00"
    "status" => 1
    "length" => null
    "width" => null
    "height" => null
    "distance_unit" => null
    "weight" => "5.00"
    "mass_unit" => "lbs"
    "created_at" => "2018-08-30 17:27:30"
    "updated_at" => "2018-09-11 14:30:54"
  ]
  #original: array:21 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▶]
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #search_bindings: []
}
1 => Product {#568 ▶}
2 => Product {#569 ▶}
]}

So what i need is to get a product whose discounted price is the highest. And in this case the discounted price is price - sale_price. As of now, I tried looping those items from the collection and tried some collection methods like filter, reduce, to get which of the products has the best discounted price but I have been unsuccessful to achieve the result. May be i am doing something wrong or i am totally missing a part.

Thank you for anyone willing to help me. :)



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2N20vel
via IFTTT

Aucun commentaire:

Enregistrer un commentaire