PHP code example of emrul1875 / laravel-extra-collection

1. Go to this page and download the library: Download emrul1875/laravel-extra-collection 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/ */

    

emrul1875 / laravel-extra-collection example snippets



Emrul1875\LaravelExtraCollection\LaravelExtraCollectionServiceProvider::class



$collection = collect([
    [
        'name' => 'John',
        'balance' => 100,
        'image' => '/uploads/image54543534.png'
    ],
    [
        'name' => 'Jonny',
        'balance' => 200
        'image' => '/uploads/image54543534.png'
    ]
]);

$updatedCollection = $collection->prependValue(["balance" => "USD ", "image": "https://dummyurl.com"], true);

/*
     [
        'name' => 'John',
        'balance' => "USD 100",
        'image' => 'https://dummyurl.com/uploads/image54543534.png'
    ],
    [
        'name' => 'Jonny',
        'balance' => "USD 200",
        'image' => 'https://dummyurl.com/uploads/image54543534.png'
    ]
*/




$collection = collect([
    [
        'name' => 'John',
        'currency' => 'FCFA',
        'balance' => 100,
    ],
    [
        'name' => 'Jonny',
        'currency' => 'FCFA',
        'balance' => 400
    ]
]);

$updatedCollection = $collection->appendValue(["balance" => " FCFA"]);

/*
    [
        'name' => 'John',
        'currency' => 'FCFA',
        'balance' => '100 FCFA',
    ],
    [
        'name' => 'Jonny',
        'currency' => 'FCFA',
        'balance' => '400 FCFA'
    ]
*/





$collection = collect([
    [
        'title' => 'Mr.'
        'firstname' => 'John',
        'lastname' => 'Doe'
    ],
    [
        'title' => 'Mr.'
        'firstname' => 'Johny',
        'lastname' => 'Doe'
    ]
]);

$updatedCollection = $collection->concatValue("fullname", ["title", "firstname", "lastname"], " ");

/*
    [
        'title' => 'Mr.'
        'firstname' => 'John',
        'lastname' => 'Doe',
        'fullname' => 'Mr. John Doe'
    ],
    [
        'title' => 'Mr.'
        'firstname' => 'Johny',
        'lastname' => 'Doe',
        'fullname' => 'Mr. Johny Doe'
    ]
*/




$collection = collect([
    6,5,7,5,2,5,7,3,3
]);

$updatedCollection = $collection->at(3)

/*
    5
*/

$collection = collect([
    [
        'title' => 'Mr.'
        'firstname' => 'John',
        'lastname' => 'Doe'
    ],
    [
        'title' => 'Mr.'
        'firstname' => 'Johny',
        'lastname' => 'Doe'
    ]
]);

$updatedCollection = $collection->at(1)

/*
    [
        'title' => 'Mr.'
        'firstname' => 'Johny',
        'lastname' => 'Doe'
    ]
*/




$collection = collect([
    6,5,7,5,2,5,7,3,3
]);

$updatedCollection = $collection->find(function($item) {
    return $item > 5;
});

/*
    6
*/

$collection = collect([
    [
        'title' => 'Mr.'
        'firstname' => 'John',
        'lastname' => 'Doe',
        'age' => 20
    ],
    [
        'title' => 'Mr.'
        'firstname' => 'Johny',
        'lastname' => 'Doe',
        'age' => 25
    ]
]);

$updatedCollection = $collection->find(function($item) {
    return $item->age > 15;
})

/*
    [
        'title' => 'Mr.'
        'firstname' => 'John',
        'lastname' => 'Doe',
        'age' => 20
    ],
*/




$collection = collect([
    6,5,7,5,2,5,7,3,3
]);

$updatedCollection = $collection->findIndex(function($item) {
    return $item > 5;
});

/*
    0
*/

$collection = collect([
    [
        'title' => 'Mr.'
        'firstname' => 'John',
        'lastname' => 'Doe',
        'age' => 20
    ],
    [
        'title' => 'Mr.'
        'firstname' => 'Johny',
        'lastname' => 'Doe',
        'age' => 25
    ]
]);

$updatedCollection = $collection->findIndex(function($item) {
    return $item->age > 20;
})

/*
    1
*/