Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Trace any Livewire interaction as input → property → render → patch. Find which property or method the wire: attribute names, and you know exactly which PHP runs.

Typing "dune" into the searchStep 1 of 4
  1. Input

    The member types, and wire:model.live="query" sends the new text.

The trap is expecting a private property to reach the view or keep its value between requests. Only public properties are shared with the view and carried from one round trip to the next.

Drills

Answer fast — trust the rule.

quick drill

Where does the search text live in the BookSearch component?

quick drill

What does wire:click="clear" do?

quick drill

How does wire:model.live differ from plain wire:model?

quick drill

After the server re-renders a component, what does Livewire do?

Remember

  • Public properties hold the state; wire: attributes call the server.
  • Trace it: input → property → render → patch.
  • Trap: only public properties reach the view and survive between requests.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA live search in PHP

Describe how a Livewire BookSearch component for Lendly works when a member types into the search box. Cover where the state lives, what wire:model.live does, and what travels between browser and server.