PHP code example of werxe / laravel-collection-macros

1. Go to this page and download the library: Download werxe/laravel-collection-macros library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

werxe / laravel-collection-macros example snippets


$collection = collect([
    'total' => 1,
]);

$collection->increment('total', 2); // 3

$collection = collect([
    'total' => 3,
]);

$collection->decrement('total', 2); // 1

$collection = collect(['d' => 'lemon', 'a' => 'orange', 'b' => 'banana', 'c' => 'apple']);

$collection->ksort(); // ['a' => 'orange', 'b' => 'banana', 'c' => 'apple', 'd' => 'lemon']

$collection = collect(['d' => 'lemon', 'a' => 'orange', 'b' => 'banana', 'c' => 'apple']);

$collection->krsort(); // ['d' => 'lemon', 'c' => 'apple', 'b' => 'banana', 'a' => 'orange']

$collection = collect([
    'name' => 'John Doe',
    'emails' => [
        '[email protected]',
        '[email protected]',
    ],
    'contacts' => [
        [
            'name' => 'Richard Tea',
            'emails' => [
                '[email protected]',
            ],
        ],
    ],
]);

// Convert the nested arrays into Collections
$convertedCollection = $collection->recursive();

// Get the contacts as a Collection
$contacts = $convertedCollection->get('contacts');