Delete repeated elements from a collection of objects

1

I got this product sales collection:

$ventas = VentasProductos::withCount('producto')
                        ->orderBy('producto_count', 'desc')
                        ->take(5)
                        ->get();

but in the view it shows me the repeated elements, I believe this line of code but it does not delete one of the repeated elements:

if($ventas){
        foreach($ventas as $key=>$venta){
           if(array_key_exists($venta->id, $ventas)){
                unset($ventas[$key]);
          }
        }
    }

But apparently I am misapplying the array_key_exists and I do not delete the repeated element.

#items: array:4 [▼
    0 => VentasProductos {#1791 ▼
      +table: "ventas_productos"
      +fillable: array:6 [▶]
      #casts: array:7 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▶]
      #original: array:10 [▼
        "id" => 8
        "cantidad" => 1
        "importe" => 6.0
        "importe_pagado" => 0.0
        "deuda" => 1
        "created_at" => null
        "updated_at" => null
        "producto_id" => 9
        "factura_id" => 11
        "producto_count" => 1
      ]
      #changes: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    1 => VentasProductos {#1792 ▼
      +table: "ventas_productos"
      +fillable: array:6 [▶]
      #casts: array:7 [▶]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▼
        "id" => 10
        "cantidad" => 1
        "importe" => 6.0
        "importe_pagado" => 2.0
        "deuda" => 1
        "created_at" => "2018-03-26"
        "updated_at" => "2018-03-27"
        "producto_id" => 9
        "factura_id" => 17
        "producto_count" => 1
      ]
      #original: array:10 [▼
        "id" => 10
        "cantidad" => 1
        "importe" => 6.0
        "importe_pagado" => 2.0
        "deuda" => 1
        "created_at" => "2018-03-26"
        "updated_at" => "2018-03-27"
        "producto_id" => 9
        "factura_id" => 17
        "producto_count" => 1
      ]
      #changes: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    2 => VentasProductos {#1793 ▶}
    3 => VentasProductos {#1794 ▶}
  ]
}

And only one should come out

    
asked by albertolg89 12.04.2018 в 22:32
source

1 answer

2

With the withCount function you get the number of elements resulting from the relationship with another table, without having to load them. The problem is that when you call it from the entity that has the relation of many to one, you can repeat elements and mainly the new property * _count that will add you to the results will always be 1. therefore I recommend you to call withCount from the entity that has the relation of 1 to many, managing to obtain non-repeated elements and the real amount of the relations with the other entity.

$prod = Productos::withCount('ventas_productos')
                  ->orderBy('ventas_productos_count', 'desc')
                  ->take(5)
                  ->get();
    
answered by 13.04.2018 / 15:28
source