$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[January 10, 2025] Laravel

Laravel Livewire: Reactive Components

Laravel Livewire: Reactive Components

────────────────────────────────────────────────────────

Laravel Livewire: Reactive Components

Tip: Create Component

php artisan make:livewire Counter

Gotcha: Public Properties Are Reactive

public $count = 0;

Changes automatically update the view.

Tip: Actions

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

Gotcha: Lifecycle Hooks

public function mount() {
    $this->count = 10;
}

Tip: Form Handling

public function save() {
    $this->validate(['name' => 'required']);
    Post::create($this->all());
}

Gotcha: File Uploads

use WithFileUploads;

Trait for handling file uploads in Livewire components.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Livewire has made reactive UI accessible to Laravel developers, but it introduces a mental model shift that's easy to underestimate. The most common issue I see: treating Livewire component properties like JavaScript state when they're really server-side state with network round-trips. Every $this->property = value triggers a network request. I teach teams to think of Livewire components as controllers that happen to render in real-time, not as frontend components.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts