How to Use wire:model in Laravel Livewire for Real-Time Data Binding

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

The wire:model directive in Laravel Livewire is used to link data in real time between the frontend (Blade templates) and the backend (Livewire components). In order to utilize wire:model properly, there are a few key elements and considerations to make:

Input Fields:

Text Inputs: To synchronize the component property with the input value while binding text inputs, using wire:model.

HTML
==========================
<input type="text" wire:model="name">
==========================


PHP
=============================
public $name;

Checkboxes:

Bind boolean values using wire:model and checkboxes.

HTML
==========================
<input type="checkbox" wire:model="isActive">
==========================


PHP
=============================
public $isActive;

Radio Buttons:

wire:model may be used to bind a single selected value from a set of possibilities for radio buttons.

HTML
==========================
<input type="radio" wire:model="status" value="active"> Active
<input type="radio" wire:model="status" value="inactive"> Inactive
==========================


PHP
=============================
public $status;

Select Dropdowns:

Use wire:model to bind the selected value of a dropdown.

HTML
==========================
<select wire:model="selectedOption">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
</select>
==========================


PHP
=============================
public $selectedOption;

Textarea:

You can bind the content of a textarea using wire:model, just like you would with text inputs.

HTML
==========================
<textarea wire:model="description"></textarea>
==========================


PHP
=============================
public $description;

Components with Child Properties:

When working with nested data or complex components, you can use dot notation to bind to nested properties.

HTML
==========================
<input type="text" wire:model.debounce.500ms="searchTerm">
==========================


PHP
=============================
public $searchTerm;

Lazy Updates

Use .lazy to update the backend only when the input loses focus.

HTML
==========================
<input type="text" wire:model.lazy="email">
==========================


PHP
=============================
public $email;

Deferred Updates:

Use .defer to update the backend only on form submission or a specific event.

HTML
==========================
<input type="text" wire:model.defer="username">
==========================


PHP
=============================
public $username;

Real-time Validation:

Combine wire:model with validation to provide instant feedback.

HTML
==========================
<input type="text" wire:model="email">
@error('email') <span class="error">{{ $message }}</span> @enderror
==========================


PHP
=============================
public $email;

public function updatedEmail()
{
    $this->validate([
        'email' => 'required|email',
    ]);
}

Why Use wire:model?

Real-time Data Binding: It allows for real-time synchronization between the UI and the server-side component state.

Simplifies Code: Reduces the need for manual event listeners and JavaScript code to handle data binding.

Interactive UIs: Enables the creation of interactive and dynamic user interfaces without full-page reloads.

Validation: Simplifies the process of real-time validation and provides immediate feedback to users.

Optimized Performance: By using modifiers like .debounce, .lazy, and .defer, you can optimize the performance and reduce unnecessary server requests.

Using wire:model effectively can greatly enhance the responsiveness and user experience of your Laravel Livewire applications.

Senior Software Development Engineer at Cotocus

Related Posts

How We Fixed a Stubborn Laravel MeiliSearch Bulk Indexing Failure (16,000+ Records)

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now If…

Read More

Laravel Search Without Docker, Queues, or Horizon

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now Modern…

Read More

Laravel Scout with Typesense vs Meilisearch

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now Modern…

Read More

SESSION_DRIVER=file vs SESSION_DRIVER=database in Laravel

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now Which…

Read More

Laravel Logging: Complete Step-by-Step Guide to Enable, Debug, Verify, and Master Logs in Any Laravel Project

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now Logging…

Read More

Complete Tutorial: Setting Up Laravel Telescope Correctly (Windows + XAMPP + Custom Domain)

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now This…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments