vendredi 7 décembre 2018

Laravel 5.7 - forget or except from collection when key could vary

Laravel's except() and forget() helpers both do what they do by key. I'm wondering if there's a slick laravel way to use either of those to exclude an item when the key is unpredictable. I have a collection that looks like this:

Collection {#469
#items: array:3 [
  0 => array:2 [
    "name" => "Bug"
    "class" => "App\Models\BugTypes\Bug"
  ]
  1 => array:2 [
    "name" => "PreBug"
    "class" => "App\Models\BugTypes\PreBug"
  ]
  2 => array:2 [
    "name" => "Request"
    "class" => "App\Models\BugTypes\Request"
  ]
]
}

and I want to always exclude PreBug. The issue is that the collection is built dynamically from namespaces and if you were to add or remove a namespace, the key for PreBug could change. Namely if you were to create a type that alphabetically precedes the letter P. So while $data->except(1); works now, I cannot rely on it. I know I could filter or map to remove it, so mostly just wondering if someone sees a more creative solution than that.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire