Delete an item from a Collection

1

I have a Collection that comes to me from the route. This Collection has a relation, detalle , which is where I want to eliminate one of the objects that arrive, exactly the "qdet_ref" => "300_1_2" . At another time I may want to eliminate another or others.

The Collection is called $cuestionario ; the detail is $cuestionario->detalle

Then both passed to Array so they can be visualized better:

$ questionnaire:

array:18 [▼
  "quest_id" => 278
  "quest_paxid" => 450
  "quest_anyfiscal" => 2015
  "quest_pdfgenlink" => "assets/arfis_pdf/QUESTIONARI-278-aa4f86fe6f2aa0f317df4f5491620d53e428512a.pdf"
  "quest_pdfsiglink" => null
  "quest_usruid" => 42
  "quest_acceptat" => "A"
  "quest_costsc" => "10.50"
  "quest_estat" => "T"
  "quest_generat_ip" => "2147483647"
  "quest_na" => 5
  "quest_empresa_id" => 1
  "quest_rlid" => null
  "quest_edad" => 43
  "created_at" => "2016-06-02 10:43:32"
  "updated_at" => null
  "deleted_at" => null
  "detalle" => array:63 [▶]
]

$ detail

array:63 [▼
  0 => array:13 [▼
    "qdet_id" => 10302
    "qdet_ref" => "300_1_1"
    "qdet_resp" => "S"
    "qdet_quest_id" => 278
    "qdet_resp_json" => null
    "qdet_imp1" => "0.00"
    "qdet_imp2" => "0.00"
    "qdet_nombre" => null
    "qdet_orden" => 20
    "qdet_pes" => "0.00"
    "created_at" => "2016-06-02 10:43:32"
    "updated_at" => null
    "deleted_at" => null
  ]
  1 => array:13 [▼
    "qdet_id" => 10303
    "qdet_ref" => "300_1_2"
    "qdet_resp" => "N"
    "qdet_quest_id" => 278
    "qdet_resp_json" => null
    "qdet_imp1" => "0.00"
    "qdet_imp2" => "0.00"
    "qdet_nombre" => null
    "qdet_orden" => 40
    "qdet_pes" => "0.00"
    "created_at" => "2016-06-02 10:43:32"
    "updated_at" => null
    "deleted_at" => null
  ]
  2 => array:13 [▶]
  3 => array:13 [▶]
  4 => array:13 [▶]
...
]

I have looked at all the methods available in Laravel to work with a Collection , I have tried some of them and I have not achieved what I intend with any of them. The, for me, most evident pull() that removes an element by its key:

$collection = collect(['product_id' => 'prod-100', 'name' => 'Desk']);

$collection->pull('name');

// 'Desk'

$collection->all();

// ['product_id' => 'prod-100']

Laravel-Spanish official documentation: pull ()

But when I apply it:

$detalle->pull('300_1_2');

It does absolutely nothing; returns the same as before. What is it that I am not doing well or what alternative do I have?

    
asked by Pedro Prada 07.03.2018 в 12:59
source

2 answers

2

You could use filter () to keep the other elements of the collection:

$nuevoDetalle = $detalle->filter(function ($item) {
    if ($item->qdet_ref != '300_1_2') {
        return $item;
    }
});

You can also achieve it with where () and other methods that are available.

    
answered by 07.03.2018 / 13:42
source
0

If what you want to do is remove an attribute / component from a collection, what you have to do is:

unset($detalle['300_1_2']); // en caso de que sea array

Or

unset($detalle->300_1_2); // en caso de que sea objeto

    
answered by 07.03.2018 в 13:19