-1

I want toa remove an item that is selected from a collection.

    $resultCollection = $collection->where('test_key','test_value')->deleteORremove();
    //$collection = [['test_key'=>'test_value','test_key2'=>'test_value'],['test_key'=>'test_value1','test_key2'=>test_value1']]
// $resultCollection = [['test_key'=>'test_value1','test_key2'=>test_value1']]

How to do that?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
reza yavari
  • 1
  • 1
  • 2

2 Answers2

0

Have a look at this:

How to unset (remove) a collection element after fetching it?

It is a bit old and I dont know which laravel version you are using so check the documentation

  • Thanks, but I wanna delect an item that a test_key equal to test_value suppose this: ``` [ ['test_key' => 'test_value' , test_key2'=>'a'],['test_key'=>'test_value2' , 'test_key2'=>'b'] ] ``` – reza yavari Nov 05 '18 at 11:16
0

According to the documentation,https://laravel.com/docs/5.7/collections#method-forget, what you need is to call the forget() method on the collection.

$collection->forget('test_key');
$resultCollection = $collection->all();
Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23
  • Thanks, but I wanna delect an item that a test_key equal to test_value suppose this: [ ['test_key' => 'test_value' , test_key2'=>'a'],['test_key'=>'test_value2' , 'test_key2'=>'b'] ] – reza yavari Nov 05 '18 at 11:13