2

I have the following Collection

     Collection {#430 ▼
  #items: array:54 [▼
    0 => {#435 ▼
      +"name": "Persona 5"
      +"cover": "cover_persona-5_playstation-3.png"
      +"bio": "This is a syno"
      +"all_nb_rank": null
      +"platform_name": "Sony Playstation 3"
      +"tag_name": "RPG"
      +"developper": "Deep Silver"
      +"publisher": "Atlus"
    }
    1 => {#437 ▼
      +"name": "Persona 5"
      +"cover": "cover_persona-5_playstation-3.png"
      +"bio": "This is a syno"
      +"all_nb_rank": null
      +"platform_name": "Sony Playstation 3"
      +"tag_name": "Turn based"
      +"developper": "Deep Silver"
      +"publisher": "Atlus"
    }
    2 => {#436 ▼
      +"name": "Persona 5"
      +"cover": "cover_persona-5_playstation-3.png"
      +"bio": "This is a syno"
      +"all_nb_rank": null
      +"platform_name": "Sony Playstation 3"
      +"tag_name": "Simulation"
      +"developper": "Deep Silver"
      +"publisher": "Atlus"
    }
//

I'd like to get into another collection the "tag_name" line and then delete it from the main collection, and remove the duplicates values in order to get something like this :

     Collection {#430 ▼
  #items: array:1 [▼
    0 => {#435 ▼
      +"name": "Persona 5"
      +"cover": "cover_persona-5_playstation-3.png"
      +"bio": "This is a syno"
      +"all_nb_rank": null
      +"platform_name": "Sony Playstation 3"
      +"developper": "Deep Silver"
      +"publisher": "Atlus"
    }

    Collection {#490 ▼
  #items: array:3 [▼
    0 => "RPG"
    1 => "Turn based"
    2 => "Simulation"
  ]
}

I already manage to get the "tag_name" line into another Collection by using

$tags = $collection->pluck('tag_name');

I also plan on using the unique() collection method in order merge the duplicates values.

But I don't know how I should process in order to remove the "tag_name" from the main collection.

I tried to use the forget() and slice() method but it doesn't work. I know I can make it into a simple array and then use the PHP unset() function but I'd like to know how to do it using Laravel Collection methods

M.Pau
  • 147
  • 1
  • 3
  • 12

3 Answers3

3

You can use forget() by doing a little trick as forget() only works on collection object.

$collections = $collections->map(function ($c) {
    return collect($c)->forget('tag_name');
});
return $collections;
Sohel0415
  • 9,523
  • 21
  • 30
  • Though you are answering the question, it was originally asking to use Laravel collection methods rather than unset(). If a Laravel collection is a super set of the PHP array and unset() is what is used to remove a property from a Laravel collection you should specify that. – SomethingOn Jan 29 '18 at 13:44
  • 1
    @SomethingOn I was missing the unset part in original question. I have updated my answer and thanks for your suggestion. – Sohel0415 Jan 29 '18 at 17:43
  • This was helpful for me, as except() wasn't working, but this did. – JessycaFrederick Feb 26 '22 at 22:00
1

The except() method does exactly what you need. It removes one or more keys from a collection:

$persona = collect([     
    'name' => 'Persona 5',
    'cover' => 'cover_persona-5_playstation-3.png',
    'bio' => 'This is a syno',
    'all_nb_rank": nul',
    'platform_name' => 'Sony Playstation 3',
    'tag_name' => 'RPG',
    'developper' => 'Deep Silver',
    'publisher' => 'Atlus'
]);

$result = $persona->except(['tag_name']);

$personasWithOutTagName = $personas->except(['tag_name']);

This will result in:

[
    'name' => 'Persona 5',
    'cover' => 'cover_persona-5_playstation-3.png',
    'bio' => 'This is a syno',
    'all_nb_rank": nul',
    'platform_name' => 'Sony Playstation 3',
    'developper' => 'Deep Silver',
    'publisher' => 'Atlus'
];

Doing this for all of your items in the personas collection, you can do this:

$result = $personas->map(function ($person) {
    return collect($person)->except(['tag_name'])->all();
});

The $result will be a collection with all personas except the tag_name key.

common sense
  • 3,775
  • 6
  • 22
  • 31
0

The partition method can be used to separate a collection into one or more collections depending on the logic of your truth test. Use it with the list method as shown in the example here:

https://laravel.com/docs/5.5/collections#method-partition

Another option is the mapToGroups method.

https://laravel.com/docs/5.5/collections#method-maptogroups