Laravel collections

méthode duplicates()

Quand est apparue cette méthode ?

depuis Laravel v5.8

Description


Exemples

Il y a 2 exemples pour cette collection

Exemple #1 : Pas de souci Boss, je vous amène ça dans 5 minutes !

Collection utilisée

    
        // define Level collection
        $this->level = collect([
            'expert',
            'normal',
            'normal',
            'newbie',
            'newbie',
            'normal',
            'newbie',
            'expert'
            ]
        );

        Illuminate\Support\Collection {#422 ▼
            #items: array:8 [▼
              0 => "expert"
              1 => "normal"
              2 => "normal"
              3 => "newbie"
              4 => "newbie"
              5 => "normal"
              6 => "newbie"
              7 => "expert"
            ]
          }
    

Code source


$level = collect([
            "expert",
            "normal",
            "normal",
            "newbie",
            "newbie",
            "normal",
            "newbie",
            "expert"
            ]
        );

var_dump($level->duplicates());

Résultat

    
object(Illuminate\Support\Collection)#1401 (1) {
  ["items":protected]=>
  array(5) {
    [2]=>
    string(6) "normal"
    [4]=>
    string(6) "newbie"
    [5]=>
    string(6) "normal"
    [6]=>
    string(6) "newbie"
    [7]=>
    string(6) "expert"
  }
}
    

Exemple #2 : Pas de souci Boss, je vous amène ça dans 5 minutes ! -- exemple avec données imbriquées

Collection utilisée

    
        // define nested collection
        $this->nested = collect([
            ['name' => 'Spaghetti à la carbonara', 'level' => 'Moyen', 'price' => 'Economique', 'type' => 'Pâtes'],
            ['name' => 'Loup entier grillé', 'level' => 'Chef', 'price' => 'Cher', 'type' => 'Poisson'],
            ['name' => 'Gnocchi au pesto', 'level' => 'Facile', 'price' => 'Economique', 'type' => 'Pâtes'],
            ['name' => 'Daurade vapeur et ses pommes de terre', 'level' => 'Moyen', 'price' => 'Moyen', 'type' => 'Poisson'],
            ['name' => 'Dame blanche', 'level' => 'Moyen', 'price' => 'Moyen', 'type' => 'Dessert'],
            ['name' => 'Banana Split', 'level' => 'Chef', 'price' => 'Cher', 'type' => 'Dessert'],
            ['name' => 'Coupe Colonel', 'level' => 'Facile', 'price' => 'Economique', 'type' => 'Dessert'],
        ]);

        Illuminate\Support\Collection {#440 ▼
            #items: array:7 [▼
              0 => array:4 [▼
                "name" => "Spaghetti à la carbonara"
                "level" => "Moyen"
                "price" => "Economique"
                "type" => "Pâtes"
              ]
              1 => array:4 [▼
                "name" => "Loup entier grillé"
                "level" => "Chef"
                "price" => "Cher"
                "type" => "Poisson"
              ]
              2 => array:4 [▼
                "name" => "Gnocchi au pesto"
                "level" => "Facile"
                "price" => "Economique"
                "type" => "Pâtes"
              ]
              3 => array:4 [▼
                "name" => "Daurade vapeur et ses pommes de terre"
                "level" => "Moyen"
                "price" => "Moyen"
                "type" => "Poisson"
              ]
              4 => array:4 [▼
                "name" => "Dame blanche"
                "level" => "Moyen"
                "price" => "Moyen"
                "type" => "Dessert"
              ]
              5 => array:4 [▼
                "name" => "Banana Split"
                "level" => "Chef"
                "price" => "Cher"
                "type" => "Dessert"
              ]
              6 => array:4 [▼
                "name" => "Coupe Colonel"
                "level" => "Facile"
                "price" => "Economique"
                "type" => "Dessert"
              ]
            ]
          }
    

Code source


$nested = collect([
            ["name" => "Spaghetti à la carbonara", "level" => "Moyen", "price" => "Economique", "type" => "Pâtes"],
            ["name" => "Loup entier grillé", "level" => "Chef", "price" => "Cher", "type" => "Poisson"],
            ["name" => "Gnocchi au pesto", "level" => "Facile", "price" => "Economique", "type" => "Pâtes"],
            ["name" => "Daurade vapeur et ses pommes de terre", "level" => "Moyen", "price" => "Moyen", "type" => "Poisson"],
            ["name" => "Dame blanche", "level" => "Moyen", "price" => "Moyen", "type" => "Dessert"],
            ["name" => "Banana Split", "level" => "Chef", "price" => "Cher", "type" => "Dessert"],
            ["name" => "Coupe Colonel", "level" => "Facile", "price" => "Economique", "type" => "Dessert"],
        ]);

// recherche des doublons sur la clé 'level'
var_dump($nested->duplicates('level'));

Résultat

    
object(Illuminate\Support\Collection)#1417 (1) {
  ["items":protected]=>
  array(4) {
    [3]=>
    string(5) "Moyen"
    [4]=>
    string(5) "Moyen"
    [5]=>
    string(4) "Chef"
    [6]=>
    string(6) "Facile"
  }
}