PHP code example of laravelldone / sql-to-signal

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

    

laravelldone / sql-to-signal example snippets


$signal = User::where('active', true)->toSignal();

class OrderDashboard extends Component
{
    // You can't store a QueryBuilder as a public property.
    // Livewire can't serialize it — it will throw or silently drop it.
    // So you have to rebuild the query from scratch on every request.

    public array $orders = [];    // you lose Collection methods
    public int   $count  = 0;
    public ?array $first = null;

    private function baseQuery(): Builder
    {
        // Duplicated every time: if filters change you must update in multiple places
        return DB::table('orders')
            ->where('status', $this->status)
            ->where('user_id', auth()->id())
            ->orderBy('created_at', 'desc');
    }

    public function mount(): void
    {
        $q = $this->baseQuery();
        $this->orders = $q->get()->toArray();       // hit 1
        $this->count  = (clone $q)->count();        // hit 2  ← extra query
        $this->first  = (clone $q)->first();        // hit 3  ← extra query
    }

    public function refresh(): void
    {
        // Rebuild everything again — same 3 queries
        $q = $this->baseQuery();
        $this->orders = $q->get()->toArray();
        $this->count  = (clone $q)->count();
        $this->first  = (clone $q)->first();
    }
}

class OrderDashboard extends Component
{
    public Signal $orders;  // serializes/hydrates automatically between requests

    public function mount(): void
    {
        // One query, one database hit. count/first/pluck come for free.
        $this->orders = DB::table('orders')
            ->where('status', $this->status)
            ->where('user_id', auth()->id())
            ->orderBy('created_at', 'desc')
            ->toSignal();
    }

    public function refresh(): void
    {
        // Re-runs the exact same SQL — no need to rebuild the query
        $this->orders = $this->orders->refresh();
    }
}

// Controller / Livewire component
$rows     = DB::table('orders')->where(...)->get()->toArray();
$count    = DB::table('orders')->where(...)->count();   // cloned query, second hit
$interval = config('dashboard.polling_interval');       // manually forwarded

return view('dashboard', compact('rows', 'count', 'interval'));

$signal = DB::table('orders')->where(...)->toSignal();

return view('dashboard', compact('signal'));

return [
    'cache' => [
        'enabled' => false,
        'ttl'     => 60, // seconds
    ],

    // Passed as meta for Alpine.js polling wiring
    'polling_interval' => 2000, // milliseconds

    // true = getData() returns a Collection, false = plain array
    'as_collection' => true,

    // Max rows allowed in a Signal (null = unlimited)
    'max_rows' => 1000,
];

use Illuminate\Support\Facades\DB;

$signal = DB::table('orders')
    ->where('status', 'pending')
    ->orderBy('created_at', 'desc')
    ->toSignal();

// $signal is a Signal instance
$signal->getQuery();
// "select * from `orders` where `status` = ? order by `created_at` desc"

$signal->getBindings();
// ["pending"]

$signal->count();
// 3

$signal->getData();
// Illuminate\Support\Collection {
//   0 => { "id": 1, "status": "pending", "total": 120.00, ... },
//   1 => { "id": 2, "status": "pending", "total": 89.50,  ... },
//   2 => { "id": 3, "status": "pending", "total": 45.00,  ... },
// }

$signal = Order::query()
    ->with('customer')
    ->where('status', 'pending')
    ->toSignal();

$signal->getModelClass();
// "App\Models\Order"

$signal->first();
// App\Models\Order { #id: 1, #status: "pending", ... }

$signal->pluck('total');
// Illuminate\Support\Collection [120.00, 89.50, 45.00]

$signal = Product::active()->toSignal([
    'polling_interval' => 5000,
    'max_rows'         => 50,
]);

$signal->toArray();
// [
//   "data" => [ ... up to 50 products ... ],
//   "meta" => [
//     "count"            => 12,
//     "model_class"      => "App\Models\Product",
//     "polling_interval" => 5000,   // <-- overridden
//     "pagination"       => null,   // null when not paginated
//   ]
// ]

use Livewire\Component;
use Laravelldone\SqlToSignal\Signal;

class OrderDashboard extends Component
{
    public Signal $orders;

    public function mount(): void
    {
        $this->orders = Order::pending()->toSignal();
    }

    public function refresh(): void
    {
        $this->orders = $this->orders->refresh();
        // Re-runs the original SQL with the same bindings.
        // No need to rebuild the query from scratch.
    }

    public function render()
    {
        return view('livewire.order-dashboard');
    }
}

public function mount(): void
{
    $this->orders = Order::pending()
        ->orderBy('created_at', 'desc')
        ->toSignal(['per_page' => 15, 'page' => 1]);
}

public function nextPage(): void { $this->orders = $this->orders->nextPage(); }
public function prevPage(): void { $this->orders = $this->orders->prevPage(); }
public function goToPage(int $page): void { $this->orders = $this->orders->goToPage($page); }

// Table has 1 500 rows — this throws immediately
$signal = Report::query()->toSignal(['max_rows' => 500]);

// OverflowException: Signal result set exceeds the configured max_rows limit
// of 500. Got 1500 rows.

// Safe — scoped
$signal = Report::thisMonth()->toSignal(['max_rows' => 500]);

// Unlimited — use with care
$signal = Report::query()->toSignal(['max_rows' => null]);
blade
<div>
    <button wire:click="refresh">Refresh</button>

    @foreach ($orders->getData() as $order)
        <div>{{ $order->id }} — {{ $order->status }}</div>
    @endforeach

    <p>Total: {{ $orders->count() }}</p>
</div>
blade
<div wire:poll.5000ms="refresh">
    @foreach ($orders->getData() as $order)
        <div>{{ $order->id }} — {{ $order->status }}</div>
    @endforeach
</div>
blade
@foreach ($orders->getData() as $order)
    <div>{{ $order->id }} — {{ $order->status }}</div>
@endforeach

<div>
    Page {{ $orders->getCurrentPage() }} of {{ $orders->getLastPage() }}
    &nbsp;·&nbsp; {{ $orders->getTotal() }} total
</div>

<button wire:click="prevPage" @disabled($orders->getCurrentPage() === 1)>← Prev</button>
<button wire:click="nextPage" @disabled($orders->getCurrentPage() === $orders->getLastPage())>Next →</button>