PHP code example of rembon / sync-collection

1. Go to this page and download the library: Download rembon/sync-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/ */

    

rembon / sync-collection example snippets


'providers' => ServiceProvider::defaultProviders()->merge([
    ...
    Rembon\SyncCollection\SyncCollectionServiceProvider::class,
    ...
])->toArray(),

use Rembon\SyncCollection\Services\BuildCollection;
use Rembon\SyncCollection\SyncCollection;

BuildCollection::set(Collection $collection, Builder $callback)

SyncCollection::withSingleBetween(Collection $old_collection, Collection $new_collection, array $unique_key_to_protect);

SyncCollection::withAssociativeBetween(Collection $old_collection, Collection $new_collection, string $unique_key);

BuildCollection::set($oldData, function ($item) {
    return $item->map(function ($val, $key) {
        return $val['quantity'] > 20;
    });
})

[
    0 => false,
    1 => false,
    2 => true
]

// Data Lama Singular
$old_data = collect([
    'id' => 1,
    'name' => 'item 1',
    'quantity' => 10,
]);

// Data Baru Singular
$new_data = collect([
    'name' => 'New Item 1',
    'quantity' => 100,
]);

return SyncCollection::withSingleBetween($old_data, $new_data, ['id']);

[
    "id" => 1,
    "name" => "New Item 1",
    "quantity" => 100
]

// Data lama
$oldData = collect([
    ['id' => 1, 'name' => 'Item 1', 'quantity' => 10],
    ['id' => 2, 'name' => 'Item 2', 'quantity' => 20],
    ['id' => 3, 'name' => 'Item 3', 'quantity' => 30],
]);

// Data baru
$newData = collect([
    ['id' => 1, 'name' => 'New Item', 'quantity' => 15],
    ['name' => 'Another New Item', 'quantity' => 25],
    ['name' => 'Another New Items 2', 'quantity' => 250],
]);

return SyncCollection::withAssociativeBetween($oldData, $newData, 'id');

[
    "currents" => [
        [
          "id" => 2, 
          "name" => "Item 2",
          "quantity" => 20
        ],
        [
          "id" => 3,
          "name" => "Item 3",
          "quantity" => 30
        ]
    ],
    "appends" => [
        [
           "name" => "Another New Item",
           "quantity" => 25
        ],
        [
           "name" => "Another New Items 2",
           "quantity" => 250
        ]
    ],
    "olds" => [
        [
           "id" => 1,
           "name" => "Item 1",
           "quantity" => 10
        ]
    ],
    "updated" => [
        [
           "id" => 1
           "name" => "New Item"
           "quantity" => 15
        ]
    ]
]