lundi 29 janvier 2018

laravel collection remove item

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire