PHPackages                             xslain/laravel-offline-sync - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. xslain/laravel-offline-sync

ActiveLibrary

xslain/laravel-offline-sync
===========================

A Laravel package for synchronizing offline and online databases with bidirectional sync capabilities

1.0.0(7mo ago)0281MITPHPPHP ^8.1CI failing

Since Sep 23Pushed 7mo agoCompare

[ Source](https://github.com/ogoungaemmanuel/webconnect)[ Packagist](https://packagist.org/packages/xslain/laravel-offline-sync)[ RSS](/packages/xslain-laravel-offline-sync/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

Laravel Offline Sync Package
============================

[](#laravel-offline-sync-package)

A comprehensive Laravel package for synchronizing data between offline and online databases with automatic conflict resolution and bidirectional sync capabilities. **Fully compatible with Laravel 10.x, 11.x, and 12.x**.

Features
--------

[](#features)

- 🔄 **Bidirectional Synchronization**: Sync data between offline (MySQL/SQL Server) and online (MySQL/PostgreSQL/SQL Server) databases
- 🚀 **Automatic Mode Switching**: Intelligently switches between offline and online modes based on connectivity
- ⚡ **Queue-based Processing**: Efficient background processing of sync operations
- 🔧 **Conflict Resolution**: Multiple strategies for handling data conflicts
- 🎯 **Model Integration**: Simple trait-based integration with your Eloquent models
- 📊 **Status Monitoring**: Comprehensive status and statistics tracking
- 🛡️ **Error Handling**: Robust error handling with retry mechanisms
- ⚙️ **Configurable**: Highly configurable to fit your specific needs

Requirements
------------

[](#requirements)

- PHP 8.1+
- Laravel 10.0+, 11.0+, or 12.0+
- MySQL/SQL Server (for offline database)
- MySQL/PostgreSQL/SQL Server (for online database)

Laravel Version Compatibility
-----------------------------

[](#laravel-version-compatibility)

Laravel VersionPackage SupportStatusLaravel 10.x✅ Full SupportStableLaravel 11.x✅ Full SupportStableLaravel 12.x✅ Full SupportLatestThe package is designed to be forward-compatible and will work with future Laravel versions that maintain backward compatibility.

Installation
------------

[](#installation)

### 1. Install the Package

[](#1-install-the-package)

```
composer require xslain/laravel-offline-sync
```

### 2. Install Database Extensions (if needed)

[](#2-install-database-extensions-if-needed)

#### For SQL Server Support

[](#for-sql-server-support)

```
# On Windows with XAMPP/WAMP
# Download Microsoft Drivers for PHP for SQL Server
# https://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server

# On Linux/Ubuntu
sudo apt-get update
sudo apt-get install php8.1-sqlsrv php8.1-pdo-sqlsrv

# On CentOS/RHEL
sudo yum install php-sqlsrv php-pdo_sqlsrv
```

#### For PostgreSQL Support

[](#for-postgresql-support)

```
# On Ubuntu/Debian
sudo apt-get install php8.1-pgsql

# On CentOS/RHEL
sudo yum install php-pgsql

# On macOS with Homebrew
brew install php@8.1-pgsql
```

### 3. Publish Configuration

[](#3-publish-configuration)

```
php artisan vendor:publish --provider="Xslain\OfflineSync\OfflineSyncServiceProvider" --tag="config"
```

### 4. Publish and Run Migrations

[](#4-publish-and-run-migrations)

```
php artisan vendor:publish --provider="Xslain\OfflineSync\OfflineSyncServiceProvider" --tag="migrations"
php artisan migrate
```

Configuration
-------------

[](#configuration)

### Database Connections

[](#database-connections)

Add your offline and online database connections to `config/database.php`. The package supports MySQL, PostgreSQL, and SQL Server:

#### MySQL Configuration

[](#mysql-configuration)

```
'connections' => [
    // Your existing connections...

    'mysql_offline' => [
        'driver' => 'mysql',
        'host' => env('DB_OFFLINE_HOST', '127.0.0.1'),
        'port' => env('DB_OFFLINE_PORT', '3306'),
        'database' => env('DB_OFFLINE_DATABASE', 'offline_db'),
        'username' => env('DB_OFFLINE_USERNAME', 'root'),
        'password' => env('DB_OFFLINE_PASSWORD', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'mysql_online' => [
        'driver' => 'mysql',
        'host' => env('DB_ONLINE_HOST', '127.0.0.1'),
        'port' => env('DB_ONLINE_PORT', '3306'),
        'database' => env('DB_ONLINE_DATABASE', 'forge'),
        'username' => env('DB_ONLINE_USERNAME', 'forge'),
        'password' => env('DB_ONLINE_PASSWORD', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
],
```

#### SQL Server Configuration

[](#sql-server-configuration)

```
'connections' => [
    // Your existing connections...

    'sqlsrv_offline' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_OFFLINE_HOST', 'localhost'),
        'port' => env('DB_OFFLINE_PORT', '1433'),
        'database' => env('DB_OFFLINE_DATABASE', 'offline_db'),
        'username' => env('DB_OFFLINE_USERNAME', 'sa'),
        'password' => env('DB_OFFLINE_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'prefix_indexes' => true,
        // SQL Server specific options
        'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', true),
        'encrypt' => env('DB_ENCRYPT', true),
        'multiple_active_result_sets' => false,
    ],

    'sqlsrv_online' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_ONLINE_HOST', 'localhost'),
        'port' => env('DB_ONLINE_PORT', '1433'),
        'database' => env('DB_ONLINE_DATABASE', 'online_db'),
        'username' => env('DB_ONLINE_USERNAME', 'sa'),
        'password' => env('DB_ONLINE_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'prefix_indexes' => true,
        'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', true),
        'encrypt' => env('DB_ENCRYPT', true),
        'multiple_active_result_sets' => false,
    ],
],
```

#### PostgreSQL Configuration

[](#postgresql-configuration)

```
'connections' => [
    // Your existing connections...

    'pgsql_offline' => [
        'driver' => 'pgsql',
        'host' => env('DB_OFFLINE_HOST', '127.0.0.1'),
        'port' => env('DB_OFFLINE_PORT', '5432'),
        'database' => env('DB_OFFLINE_DATABASE', 'offline_db'),
        'username' => env('DB_OFFLINE_USERNAME', 'postgres'),
        'password' => env('DB_OFFLINE_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'prefix_indexes' => true,
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],

    'pgsql_online' => [
        'driver' => 'pgsql',
        'host' => env('DB_ONLINE_HOST', '127.0.0.1'),
        'port' => env('DB_ONLINE_PORT', '5432'),
        'database' => env('DB_ONLINE_DATABASE', 'online_db'),
        'username' => env('DB_ONLINE_USERNAME', 'postgres'),
        'password' => env('DB_ONLINE_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'prefix_indexes' => true,
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],
],
```

### Environment Variables

[](#environment-variables)

Add these variables to your `.env` file based on your database choice:

#### Mixed Database Configuration

[](#mixed-database-configuration)

The package supports mixing different database types for offline and online connections. This allows you to use the best database for each scenario.

**Example 1: MySQL Offline → SQL Server Online**

```
# Offline Database (MySQL)
OFFLINE_SYNC_OFFLINE_CONNECTION=mysql_offline
DB_MYSQL_OFFLINE_HOST=localhost
DB_MYSQL_OFFLINE_PORT=3306
DB_MYSQL_OFFLINE_DATABASE=offline_db
DB_MYSQL_OFFLINE_USERNAME=root
DB_MYSQL_OFFLINE_PASSWORD=password

# Online Database (SQL Server)
OFFLINE_SYNC_ONLINE_CONNECTION=sqlsrv_online
DB_SQLSRV_ONLINE_HOST=server.example.com
DB_SQLSRV_ONLINE_PORT=1433
DB_SQLSRV_ONLINE_DATABASE=online_db
DB_SQLSRV_ONLINE_USERNAME=sa
DB_SQLSRV_ONLINE_PASSWORD=password
```

**Example 2: SQL Server Offline → MySQL Online**

```
# Offline Database (SQL Server)
OFFLINE_SYNC_OFFLINE_CONNECTION=sqlsrv_offline
DB_SQLSRV_OFFLINE_HOST=localhost
DB_SQLSRV_OFFLINE_PORT=1433
DB_SQLSRV_OFFLINE_DATABASE=offline_db
DB_SQLSRV_OFFLINE_USERNAME=sa
DB_SQLSRV_OFFLINE_PASSWORD=password

# Online Database (MySQL)
OFFLINE_SYNC_ONLINE_CONNECTION=mysql_online
DB_MYSQL_ONLINE_HOST=cloud.mysql.com
DB_MYSQL_ONLINE_PORT=3306
DB_MYSQL_ONLINE_DATABASE=online_db
DB_MYSQL_ONLINE_USERNAME=user
DB_MYSQL_ONLINE_PASSWORD=password
```

#### For MySQL

[](#for-mysql)

```
# Offline Sync Configuration
OFFLINE_SYNC_DEFAULT_MODE=auto
OFFLINE_SYNC_OFFLINE_CONNECTION=mysql_offline
OFFLINE_SYNC_ONLINE_CONNECTION=mysql_online

# Connectivity Settings
OFFLINE_SYNC_CHECK_INTERVAL=30
OFFLINE_SYNC_TIMEOUT=5
OFFLINE_SYNC_RETRY_ATTEMPTS=3

# Sync Settings
OFFLINE_SYNC_AUTO_SYNC=true
OFFLINE_SYNC_INTERVAL=60
OFFLINE_SYNC_BATCH_SIZE=100
OFFLINE_SYNC_MAX_RETRIES=3
OFFLINE_SYNC_CONFLICT_RESOLUTION=latest_wins

# Queue Settings
OFFLINE_SYNC_QUEUE_CONNECTION=database
OFFLINE_SYNC_QUEUE_NAME=offline-sync

# Logging
OFFLINE_SYNC_LOGGING_ENABLED=true
OFFLINE_SYNC_LOGGING_LEVEL=info
OFFLINE_SYNC_LOGGING_CHANNEL=offline-sync

# Offline Database Connection (MySQL)
DB_OFFLINE_HOST=127.0.0.1
DB_OFFLINE_PORT=3306
DB_OFFLINE_DATABASE=offline_db
DB_OFFLINE_USERNAME=root
DB_OFFLINE_PASSWORD=your-offline-password

# Online Database Connection (MySQL)
DB_ONLINE_HOST=your-online-host
DB_ONLINE_PORT=3306
DB_ONLINE_DATABASE=your-online-database
DB_ONLINE_USERNAME=your-online-username
DB_ONLINE_PASSWORD=your-online-password
```

#### For SQL Server

[](#for-sql-server)

```
# Offline Sync Configuration
OFFLINE_SYNC_DEFAULT_MODE=auto
OFFLINE_SYNC_OFFLINE_CONNECTION=sqlsrv_offline
OFFLINE_SYNC_ONLINE_CONNECTION=sqlsrv_online

# Connectivity Settings
OFFLINE_SYNC_CHECK_INTERVAL=30
OFFLINE_SYNC_TIMEOUT=5
OFFLINE_SYNC_RETRY_ATTEMPTS=3

# Sync Settings
OFFLINE_SYNC_AUTO_SYNC=true
OFFLINE_SYNC_INTERVAL=60
OFFLINE_SYNC_BATCH_SIZE=100
OFFLINE_SYNC_MAX_RETRIES=3
OFFLINE_SYNC_CONFLICT_RESOLUTION=latest_wins

# Queue Settings
OFFLINE_SYNC_QUEUE_CONNECTION=database
OFFLINE_SYNC_QUEUE_NAME=offline-sync

# Logging
OFFLINE_SYNC_LOGGING_ENABLED=true
OFFLINE_SYNC_LOGGING_LEVEL=info
OFFLINE_SYNC_LOGGING_CHANNEL=offline-sync

# Offline Database Connection (SQL Server)
DB_OFFLINE_HOST=localhost
DB_OFFLINE_PORT=1433
DB_OFFLINE_DATABASE=offline_db
DB_OFFLINE_USERNAME=sa
DB_OFFLINE_PASSWORD=your-offline-password
DB_TRUST_SERVER_CERTIFICATE=true
DB_ENCRYPT=true

# Online Database Connection (SQL Server)
DB_ONLINE_HOST=your-online-host
DB_ONLINE_PORT=1433
DB_ONLINE_DATABASE=your-online-database
DB_ONLINE_USERNAME=your-online-username
DB_ONLINE_PASSWORD=your-online-password
```

#### For PostgreSQL

[](#for-postgresql)

```
# Offline Sync Configuration
OFFLINE_SYNC_DEFAULT_MODE=auto
OFFLINE_SYNC_OFFLINE_CONNECTION=pgsql_offline
OFFLINE_SYNC_ONLINE_CONNECTION=pgsql_online

# Connectivity Settings
OFFLINE_SYNC_CHECK_INTERVAL=30
OFFLINE_SYNC_TIMEOUT=5
OFFLINE_SYNC_RETRY_ATTEMPTS=3

# Sync Settings
OFFLINE_SYNC_AUTO_SYNC=true
OFFLINE_SYNC_INTERVAL=60
OFFLINE_SYNC_BATCH_SIZE=100
OFFLINE_SYNC_MAX_RETRIES=3
OFFLINE_SYNC_CONFLICT_RESOLUTION=latest_wins

# Queue Settings
OFFLINE_SYNC_QUEUE_CONNECTION=database
OFFLINE_SYNC_QUEUE_NAME=offline-sync

# Logging
OFFLINE_SYNC_LOGGING_ENABLED=true
OFFLINE_SYNC_LOGGING_LEVEL=info
OFFLINE_SYNC_LOGGING_CHANNEL=offline-sync

# Offline Database Connection (PostgreSQL)
DB_OFFLINE_HOST=127.0.0.1
DB_OFFLINE_PORT=5432
DB_OFFLINE_DATABASE=offline_db
DB_OFFLINE_USERNAME=postgres
DB_OFFLINE_PASSWORD=your-offline-password

# Online Database Connection (PostgreSQL)
DB_ONLINE_HOST=your-online-host
DB_ONLINE_PORT=5432
DB_ONLINE_DATABASE=your-online-database
DB_ONLINE_USERNAME=your-online-username
DB_ONLINE_PASSWORD=your-online-password
```

### Model Configuration

[](#model-configuration)

Configure which models should be synchronized in `config/offline-sync.php`:

```
'models' => [
    App\Models\User::class => [
        'sync_fields' => ['name', 'email', 'updated_at'],
        'exclude_fields' => ['password', 'remember_token'],
        'sync_deletes' => true,
        'priority' => 1,
    ],
    App\Models\Post::class => [
        'sync_fields' => ['title', 'content', 'published_at', 'updated_at'],
        'exclude_fields' => [],
        'sync_deletes' => true,
        'priority' => 2,
    ],
],
```

Usage
-----

[](#usage)

### Mixed Database Support

[](#mixed-database-support)

The package automatically handles data type conversions when synchronizing between different database systems (MySQL ↔ SQL Server ↔ PostgreSQL).

**Automatic Conversions Include:**

- **Boolean Values**: Converted to appropriate format for target database
- **DateTime Formats**: Adjusted for database-specific precision and format
- **JSON Data**: Handled natively where supported, converted as needed
- **Binary Data**: Properly encoded for target database (hex format, etc.)
- **Character Encoding**: UTF-8 compatibility across all systems

**Supported Mixed Configurations:**

- MySQL ↔ SQL Server
- MySQL ↔ PostgreSQL
- SQL Server ↔ PostgreSQL
- Any combination of the above

### Making Models Syncable

[](#making-models-syncable)

Add the `Syncable` trait to your models:

```
