PHP code example of mwguerra / wire-bridge

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

    

mwguerra / wire-bridge example snippets




namespace App\Livewire;

use Livewire\Attributes\Layout;
use Livewire\Attributes\Session;
use Livewire\Component;

class TodoList extends Component
{
    #[Session]
    public string $title = '';

    #[Session]
    public int $count = 0;

    public function increment(): void
    {
        $this->count++;
    }

    #[Layout('layouts.app')]
    public function render()
    {
        return view('livewire.todo-list');
    }
}

// routes/web.php
use App\Livewire\TodoList;

Route::get('/todos', TodoList::class);


// app/Livewire/TodoApp.php

namespace App\Livewire;

use Livewire\Attributes\Layout;
use Livewire\Attributes\Session;
use Livewire\Component;

class TodoApp extends Component
{
    #[Session]
    public string $title = 'My Todos';

    #[Session]
    public array $items = [];

    #[Session]
    public string $filter = 'all'; // all, active, done

    public function addItem(string $label): void
    {
        $this->items[] = [
            'id'    => uniqid(),
            'label' => $label,
            'done'  => false,
        ];
    }

    public function toggleItem(string $id): void
    {
        foreach ($this->items as &$item) {
            if ($item['id'] === $id) {
                $item['done'] = ! $item['done'];
            }
        }
    }

    public function removeItem(string $id): void
    {
        $this->items = array_values(
            array_filter($this->items, fn ($item) => $item['id'] !== $id)
        );
    }

    public function clearDone(): void
    {
        $this->items = array_values(
            array_filter($this->items, fn ($item) => ! $item['done'])
        );
    }

    #[Layout('layouts.app')]
    public function render()
    {
        return view('livewire.todo-app');
    }
}



namespace App\Filament\Widgets;

use Livewire\Attributes\Session;
use MWGuerra\WireBridge\Filament\WireBridgeWidget;

class RevenueChart extends WireBridgeWidget
{
    // Must match the registered JS component name
    protected static string $bridgeComponent = 'revenue-chart';

    // Optional: customize mount element, persistence, column span
    protected static string $bridgeEl = '#js-app';
    protected static bool $bridgePersist = true;
    protected int | string | array $columnSpan = 'full';

    #[Session]
    public array $data = [];

    #[Session]
    public string $period = 'month';

    public function loadData(): array
    {
        return \App\Models\Revenue::forPeriod($this->period)->get()->toArray();
    }

    public function setPeriod(string $period): void
    {
        $this->period = $period;
    }
}

// config/wire-bridge.php
'persist' => false,

// config/wire-bridge.php
return [
    // Global default for state persistence (true = sessionStorage enabled)
    'persist' => true,

    // Where JS bridge files are published
    'js_path' => 'resources/js/livewire-bridge',
];

use MWGuerra\WireBridge\Filament\WireBridgeWidget;

class MyWidget extends WireBridgeWidget
{
    protected static string $bridgeComponent = 'my-widget';  // JS component name
    protected static string $bridgeEl = '#js-app';           // mount selector
    protected static bool $bridgePersist = true;             // persistence
    protected int | string | array $columnSpan = 'full';     // Filament column span
}
bash
php artisan make:wire-bridge TodoList --vue
php artisan make:wire-bridge Dashboard --react
php artisan make:wire-bridge RevenueChart --vue --filament
bash
php artisan wire-bridge:install --vue      # Vue only
php artisan wire-bridge:install --react    # React only
php artisan wire-bridge:install --both     # Both frameworks
bash
php artisan make:wire-bridge TodoList --vue
bash
npm run dev
php artisan serve
bash
php artisan wire-bridge:install --vue        # Vue deps
php artisan wire-bridge:install --react      # React deps
php artisan wire-bridge:install --both       # Both
php artisan wire-bridge:install --no-deps    # Skip npm install
bash
php artisan make:wire-bridge RevenueChart --vue --filament
bash
php artisan vendor:publish --tag=wire-bridge-config