PHP code example of hub-io / laravel-hubio-sync

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

    

hub-io / laravel-hubio-sync example snippets


return [
    'api_url' => rtrim((string) env('HUBIO_SYNC_URL', ''), '/'),

    // Start a background sync after local changes and periodically while a screen polls.
    'background' => (bool) env('HUBIO_SYNC_BACKGROUND', true),
    'sync_interval_seconds' => (int) env('HUBIO_SYNC_INTERVAL', 60),
    'timeout_seconds' => 20,

    // Synchronized models by record type, in dependency order (referenced rows first).
    'models' => [],

    // Invokable class returning a stable device identity, so a reinstalled app gets its backup back.
    'identity' => null,

    // Invokable class called with a Household the device left or lost, before it is forgotten.
    'forgetting' => null,

    'default_device_name' => 'Môj telefón',

    // Downloaded photos of other devices: disk and directory of the stored relative path.
    'files' => [
        'disk' => 'local',
        'directory' => 'sync-files',
    ],
];

use Hubio\Sync\Concerns\Syncable;
use Hubio\Sync\Models\Household;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use Syncable;

    protected $fillable = ['name', 'color'];

    /** Payload uploaded to the server (reference other rows by UUID or natural key). */
    public function syncData(): array
    {
        return ['name' => $this->name, 'color' => $this->color];
    }

    /** Local columns built from a downloaded payload (null skips the record for now). */
    public static function syncAttributes(array $data, Household $household): ?array
    {
        return ['name' => (string) ($data['name'] ?? ''), 'color' => $data['color'] ?? '#6B7280'];
    }
}

'models' => [
    'categories' => Category::class,
    'products' => Product::class,
],

use Hubio\Sync\SyncTask;

SyncTask::requestIfDue();

use Hubio\Sync\SyncClient;

$result = app(SyncClient::class)->run(); // ['status' => 'synced', 'rows' => 12]

use Hubio\Sync\SyncClient;
use Hubio\Sync\Models\Household;

$sync = app(SyncClient::class);

$household = $sync->createHousehold('Family');
$household->formattedInviteCode(); // ABCD-EFGH

$sync->joinHousehold('ABCD-EFGH');
$sync->rotateInviteCode($household);
$sync->leaveHousehold($household);

Household::personalUuid(); // the device's private backup
Household::sharedUuids();

use Hubio\Sync\Shares;

$shares = app(Shares::class);

$code = $shares->share($recipe);   // ABCD2345
Shares::format($code);             // ABCD-2345

$shared = $shares->open('ABCD-2345');
$shares->rate('ABCD-2345', 5, 'Delicious');
$shares->summaries();               // ratings of the records this device shared
$shares->unshare($recipe);
bash
php artisan migrate
bash
php artisan vendor:publish --tag="hubio-sync-config"