Skip to the workspace
Pebellslearn by building
Starter Kits  ›  Lesson

Livewire — Interactive Pages in PHP

Livewire 12 minA Livewire component is a PHP class plus a Blade view whose public properties hold state, and browser actions call its methods over small server round trips that re-render only the changed HTML.

laravel / Livewire — Interactive Pages in PHP

Explore each scene at your own pace.

01 · The rule

The rule

Livewire — Interactive Pages in PHP

Lendly's book list should filter as a member types, with no page reload. Normally that means writing JavaScript. Livewire lets you build it in PHP and Blade instead: the browser sends each change to the server, and the server sends back the updated HTML.

The rule

Public properties hold the state; wire: attributes call the server.

A class-based Livewire component is two files. A PHP class holds the state and the actions:

namespace App\Livewire;

use App\Models\Book;
use Livewire\Component;

class BookSearch extends Component
{
    public string $query = '';

    public function clear()
    {
        $this->query = '';
    }

    public function render()
    {
        return view('livewire.book-search', [
            'books' => Book::where('title', 'like', "%{$this->query}%")->get(),
        ]);
    }
}

A Blade view draws it and wires the browser to the class:

<div>
    <input wire:model.live="query" placeholder="Search books">
    <button wire:click="clear">Clear</button>
    @foreach ($books as $book)
        <p>{{ $book->title }}</p>
    @endforeach
</div>
  • public properties are the component's state, and the view can read them.
  • wire:model binds an input to a property. Plain wire:model syncs when the next action runs. .live syncs as you type.
  • wire:click calls a public method on the class.

You drop the component into any page with <livewire:book-search />.

Each interaction is one round trip. The browser sends the component's current state and the action, the server runs it and calls render() again, and Livewire updates only the parts of the page that changed.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.