---
name: laravel-livewire
description: "Full-stack Laravel framework for building dynamic, reactive interfaces using PHP without writing JavaScript. Use when creating or modifying Livewire components, implementing data binding with wire:model, working with lifecycle hooks, building forms with validation, handling events and parent-child communication, implementing file uploads/pagination/lazy loading, writing tests, or optimizing performance. Supports Laravel Livewire v4+ development. Keywords: Livewire, wire:model, wire:click, livewire component, Alpine.js integration, wire:submit, real-time validation, computed properties, Laravel Livewire."
---
# Laravel Livewire
## Overview
Build dynamic, reactive Laravel interfaces using only PHP. Livewire v4+ handles server-side rendering with automatic client-side updates via hydration/dehydration—no JavaScript required.
## Quick Start
### Installation
```bash
composer require livewire/livewire
php artisan livewire:layout # Creates resources/views/layouts/app.blade.php
```
### Create a Component
```bash
# Single-file component (default)
php artisan make:livewire CreatePost
# Page component
php artisan make:livewire pages::post.create
# Multi-file component
php artisan make:livewire CreatePost --mfc
# Class-based component (traditional)
php artisan make:livewire CreatePost --class
```
### Basic Component Pattern
```php
validate([
'title' => 'required|max:255',
'content' => 'required',
]);
Post::create($this->only(['title', 'content']));
return $this->redirect('/posts');
}
};
?>
```
```blade
```
## Core Concepts
### Component Structure
**Single-file components** (`resources/views/components/post/⚡create.blade.php`):
- PHP class and Blade template in one file
- Lightning bolt (⚡) is optional and can be disabled in config
**Multi-file components** (`resources/views/components/post/⚡create/`):
- Separate files for PHP, Blade, JS, CSS
- Better for large components with significant JavaScript
**Class-based components** (`app/Livewire/CreatePost.php`):
- Traditional Laravel structure
- Familiar for Livewire v2/v3 developers
### Property Management
```php
// Public properties - accessible in template as $property
public $title = '';
// Protected properties - accessible as $this->property, not sent to client
protected $apiKey = 'secret';
// Typed properties
public string $email = '';
public int $count = 0;
public ?Post $post; // Auto-locks ID
// Reset properties
$this->reset('title', 'content');
$value = $this->pull('title'); // Get and reset
```
### Lifecycle Hooks
| Hook | When It Runs |
|------|--------------|
| `mount()` | First load only - receive props/route params |
| `boot()` | Every request (initial + subsequent) |
| `hydrate()` | Beginning of subsequent requests |
| `dehydrate()` | End of every request |
| `updating($prop)` | Before property update |
| `updated($prop)` | After property update |
| `rendering()` | Before render() |
| `rendered()` | After render() |
| `exception($e)` | When exception thrown |
```php
public function mount(Post $post)
{
$this->post = $post;
$this->title = $post->title;
}
public function updatedTitle($value)
{
$this->title = strtolower($value);
}
```
### Computed Properties
Memoized derived values—accessed via `$this->property`.
```php
use Livewire\Attributes\Computed;
#[Computed]
public function posts()
{
return Post::all(); // Runs once per request
}
#[Computed(persist: true, seconds: 3600)]
public function cachedData()
{
return ExpensiveModel::all();
}
```
Usage in blade: `@foreach ($this->posts as $post)`
## Data Binding
### wire:model Modifiers
| Modifier | Behavior |
|----------|----------|
| (default) | Updates only on action submit |
| `.live` | Updates as user types (150ms debounce) |
| `.blur` | Updates when user clicks away |
| `.change` | Updates immediately on selection |
| `.debounce.500ms` | Custom debounce duration |
| `.number` | Cast to int on server |
| `.boolean` | Cast to bool on server |
```blade
```
### Dependent Selects (Important!)
Use `wire:key` when one select depends on another.
```blade
```
## Actions & Events
### Event Listeners
```blade