1. Go to this page and download the library: Download joshcirre/duo 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/ */
joshcirre / duo example snippets
use JoshCirre\Duo\Syncable;
class Todo extends Model
{
use Syncable;
protected $fillable = ['title', 'description', 'completed'];
}
class Todo extends Model
{
use Syncable;
// ✅ CORRECT: user_id is NOT in $fillable (security)
protected $fillable = ['title', 'description', 'completed'];
// ✅ Add user relationship - Duo auto-assigns user_id during sync
public function user()
{
return $this->belongsTo(User::class);
}
}
use Livewire\Volt\Component;
use App\Models\Todo;
use JoshCirre\Duo\WithDuo;
new class extends Component {
use WithDuo; // ✨ This is all you need!
public string $newTodoTitle = '';
public function addTodo()
{
Todo::create(['title' => $this->newTodoTitle]);
$this->reset('newTodoTitle');
}
public function toggleTodo($id)
{
$todo = Todo::findOrFail($id);
$todo->update(['completed' => !$todo->completed]);
}
public function deleteTodo($id)
{
Todo::findOrFail($id)->delete();
}
public function with()
{
return ['todos' => Todo::latest()->get()];
}
};
namespace App\Livewire;
use Livewire\Component;
use App\Models\Todo;
use JoshCirre\Duo\WithDuo;
class TodoList extends Component
{
use WithDuo; // ✨ Add this trait
public string $newTodoTitle = '';
public function addTodo()
{
Todo::create(['title' => $this->newTodoTitle]);
$this->reset('newTodoTitle');
}
public function render()
{
return view('livewire.todo-list', [
'todos' => Todo::latest()->get(),
]);
}
}
use Livewire\Volt\Component;
use JoshCirre\Duo\{WithDuo, DuoConfig};
new class extends Component {
use WithDuo;
// Type-safe configuration with IDE autocomplete!
protected function duoConfig(): DuoConfig
{
return DuoConfig::make(
syncInterval: 3000,
timestampRefreshInterval: 5000,
debug: true
);
}
// ... rest of your component
}
namespace App\Livewire;
use Livewire\Component;
use JoshCirre\Duo\{WithDuo, DuoConfig};
class TodoList extends Component
{
use WithDuo;
protected function duoConfig(): DuoConfig
{
return DuoConfig::make(
timestampRefreshInterval: 30000,
maxRetryAttempts: 5
);
}
// ... rest of your component
}
protected function duoConfig(): DuoConfig
{
return DuoConfig::make(
syncInterval: 3000, // Sync every 3 seconds
timestampRefreshInterval: 5000, // Refresh timestamps every 5 seconds
maxRetryAttempts: 5, // Retry failed syncs 5 times
debug: true // Enable debug logging
);
}
use Livewire\Attributes\On;
#[On('duo-synced')]
public function handleSyncComplete()
{
// Refresh data, show notification, etc.
$this->dispatch('notify', message: 'Changes synced!');
}
use JoshCirre\Duo\WithDuo;
class MyComponent extends Component {
use WithDuo; // Make sure this is here
}
bash
# Terminal 1: Vite
npm run dev
# Terminal 2: Laravel
php artisan serve