Laravel collections

méthode last()

Quand est apparue cette méthode ?

depuis Laravel v5.1

Description


Exemples

Il y a 2 exemples pour cette collection

Exemple #1 : Test sur la collection complexe, test si il y a une chaîne dans cette collection

Collection utilisée

    
        // define complexe collection
        $this->complexe = collect(
            [
                ['name' => 'php',
                'python',
                'javascript',
                'go',
                'c#',
                'java',
                'cobol',
                'basic'],
                [-2, 200.3, -7.8, 400.1],
                ['ref' => 'XZ42', 'price' => 200.7, 'tags' => ['red', 'new']],
                'totalprice' => 422
            ]
        );

        Illuminate\Support\Collection {#423 ▼
            #items: array:4 [▼
              0 => array:8 [▼
                "name" => "php"
                0 => "python"
                1 => "javascript"
                2 => "go"
                3 => "c#"
                4 => "java"
                5 => "cobol"
                6 => "basic"
              ]
              1 => array:4 [▼
                0 => -2
                1 => 200.3
                2 => -7.8
                3 => 400.1
              ]
              2 => array:3 [▼
                "ref" => "XZ42"
                "price" => 200.7
                "tags" => array:2 [▼
                  0 => "red"
                  1 => "new"
                ]
              ]
              "totalprice" => 422
            ]
          }
    

Code source


$complexe = collect(
            [
                ["name" => "php",
                "python",
                "javascript",
                "go",
                "c#",
                "java",
                "cobol",
                "basic"],
                [-2, 200.3, -7.8, 400.1],
                ["ref" => "XZ42", "price" => 200.7, "tags" => ["red", "new"]],
                "totalprice" => 422
            ]
        );

$testItemsAreString = $complexe->last(function ($value, $key) {
    return is_string($value);
});

// retourne null car aucun élément de cette collection n'est une chaîne
var_dump($testItemsAreString);

Résultat

    
NULL
    

Exemple #2 : Test sur la collection complexe, test si il y a un tableau dans cette collection

Collection utilisée

    
        // define complexe collection
        $this->complexe = collect(
            [
                ['name' => 'php',
                'python',
                'javascript',
                'go',
                'c#',
                'java',
                'cobol',
                'basic'],
                [-2, 200.3, -7.8, 400.1],
                ['ref' => 'XZ42', 'price' => 200.7, 'tags' => ['red', 'new']],
                'totalprice' => 422
            ]
        );

        Illuminate\Support\Collection {#423 ▼
            #items: array:4 [▼
              0 => array:8 [▼
                "name" => "php"
                0 => "python"
                1 => "javascript"
                2 => "go"
                3 => "c#"
                4 => "java"
                5 => "cobol"
                6 => "basic"
              ]
              1 => array:4 [▼
                0 => -2
                1 => 200.3
                2 => -7.8
                3 => 400.1
              ]
              2 => array:3 [▼
                "ref" => "XZ42"
                "price" => 200.7
                "tags" => array:2 [▼
                  0 => "red"
                  1 => "new"
                ]
              ]
              "totalprice" => 422
            ]
          }
    

Code source


$complexe = collect(
            [
                ["name" => "php",
                "python",
                "javascript",
                "go",
                "c#",
                "java",
                "cobol",
                "basic"],
                [-2, 200.3, -7.8, 400.1],
                ["ref" => "XZ42", "price" => 200.7, "tags" => ["red", "new"]],
                "totalprice" => 422
            ]
        );

$testItemsAreArray = $complexe->last(function ($value, $key) {
    return is_array($value);
});

var_dump($testItemsAreArray);

Résultat

    
array(3) {
  ["ref"]=>
  string(4) "XZ42"
  ["price"]=>
  float(200.7)
  ["tags"]=>
  array(2) {
    [0]=>
    string(3) "red"
    [1]=>
    string(3) "new"
  }
}