Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JoaoPedro's avatar

Livewire two way data binding

How can i make to change a value inside a livewire component and update the value in the input inside a blade component?

I"m trying to do that with a input with wire.model.live="estado"

and changing the value of it inside the component with this method #[On('set-user-coordinates')] public function setUserCoordinates($latitude, $longitude) { $apiKey = env("GOOGLE_MAPS_API_KEY"); $latlng = "$latitude,$longitude";

    $response = Http::get("https://maps.googleapis.com/maps/api/geocode/json", [
        "latlng" => $latlng,
        "key" => $apiKey
    ]);
    if($response->successful()) {
        $response->json()["results"][0];
        $this->estado = $response->json()["results"][0]["address_components"][4]["short_name"];
        $this->cidade = $response->json()["results"][0]["address_components"][3]["long_name"];
    }
}

But its not working

0 likes
7 replies
LaryAI's avatar
Level 58

To achieve two-way data binding in Livewire, you need to ensure that the property you are binding to the input field is properly updated within the Livewire component. From your description, it looks like you are trying to update the estado property based on some API response.

Here is a step-by-step solution to ensure that the value inside the Livewire component updates the input field in the Blade component:

  1. Ensure the Livewire Component is Set Up Correctly:

    • Make sure you have the estado property defined in your Livewire component.
    • Use the @livewire directive to include the Livewire component in your Blade view.
  2. Update the Livewire Component:

    • Define the estado property.
    • Use the @On directive to listen for the set-user-coordinates event.
    • Update the estado property within the setUserCoordinates method.
  3. Bind the Input Field to the Livewire Property:

    • Use the wire:model.live directive to bind the input field to the estado property.

Here is an example of how you can achieve this:

Livewire Component (e.g., UserCoordinatesComponent.php)

Blade View (e.g., user-coordinates-component.blade.php)

<div>
    <input type="text" wire:model.live="estado" placeholder="Estado">
</div>

Including the Livewire Component in Another Blade View

@livewire('user-coordinates-component')

Triggering the Event

To trigger the setUserCoordinates event, you can use JavaScript or another Livewire component. Here is an example using JavaScript:

document.addEventListener('DOMContentLoaded', function () {
    Livewire.emit('setUserCoordinates', latitude, longitude);
});

Summary

  • Ensure the estado property is defined in the Livewire component.
  • Use the wire:model.live directive to bind the input field to the estado property.
  • Update the estado property within the setUserCoordinates method.
  • Trigger the setUserCoordinates event to update the estado property.

This setup should ensure that the value inside the Livewire component updates the input field in the Blade component.

Snapey's avatar

How do you suggest you update the property of the component?

JoaoPedro's avatar

@Snapey I"m calling the method inside that view with the form here document.addEventListener('livewire:init', function () { window.onload = function () { var startPos; var geoSuccess = function (position) { startPos = position; Livewire.dispatch("set-user-coordinates", {latitude: startPos.coords.latitude, longitude: startPos.coords.longitude}); }; navigator.geolocation.getCurrentPosition(geoSuccess); }; });

JoaoPedro's avatar

@Snapey Yes, its getting hit, and the value is being updated, the problem is that the value showed in the input on the frontend is not being updated, so the input has the value correctly, but its not showing

JoaoPedro's avatar

@Snapey Ok, i think i found the problem, i had a script with src="https://unpkg.com/[email protected]/dist/cdn.min.js" in my layout file and as i"m using livewire 3 i shouldn't have it, after removing it the two way data binding worked correctly

Snapey's avatar

@JoaoPedro Many have had a problem with loading alpine twice. Its a pity Alpine itself cannot warn you.

1 like

Please or to participate in this conversation.