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

tuursw's avatar

Livewire not updating the value of the input field

I have build an address geocoordinates lookup component in livewire. First the address field does a google autocomplete, then dispatches an event "updateAddress", in the livewire controller there is a listener on "updateAddress" that retrieves the geocoordinates (longitude & latitude) for this address. This all works fine... But in the blade-component the input fields with values for longitude & latitude are not updated. As a test I have outputted the values for longitude & latitude and they are correctly updated.

Anyone has an idea what might cause this?

The blade file:

<div class="sm:gap-4 sm:py-3">
    <x-label for="address" value="{{ __('Address') }}"/>
    <x-input id="address" name="address" type="text" wire:model="address"/>
    <p>{{ $address }}</p>
</div>

<div class="grid grid-cols-2 sm:gap-4 sm:py-3">
    <div class="sm:gap-4 sm:py-3">
        <x-label for="latitude" value="{{ __('Latitude') }}"/>
        <x-input type="text" wire:model="latitude"/>
        <p>{{ $latitude  }}</p>
    </div>

    <div class="sm:gap-4 sm:py-3">
        <x-label for="longitude" value="{{ __('Longitude') }}"/>
        <x-input type="text" wire:model="longitude"/>
        {{ $longitude }}
    </div>
</div>

The javascript to trigger the google lookup:

google.maps.event.addDomListener(window, 'load', initialize);

function initialize() {
    var address = document.getElementById('address');
    var autocompleteTo = new google.maps.places.Autocomplete(address);
    google.maps.event.addListener(autocompleteTo, 'place_changed', function () {
        const place = autocompleteTo.getPlace();
        $wire.dispatch('updateAddress', {address: place.formatted_address});
    });
}

And my livewire component controller:

public $address = '';
public $latitude = '';
public $longitude = '';

protected $listeners = [
    'updateAddress'
];

public function mount(){
    $this->address = 'Steentstraat 23, Brugge, Belgium';
    $this->getCoordinates($this->address);
}

public function render()
{
    return view('livewire.geocoordinates-lookup');
}

public function updateAddress($address)
{
    //dd($value);
    $this->address = $address;
    $this->getCoordinates($address);
}

public function getCoordinates($address)
{
    if (!$address) return [];

    $prepAddr = str_replace(' ', '+', $address);
    $geocode = file_get_contents('https://maps.google.com/maps/api/geocode/json?address=' . $prepAddr . '&sensor=false&key=' . env('GOOGLE_MAP_KEY'));

    $output = json_decode($geocode);

    $this->latitude = $output->results[0]->geometry->location->lat;
    $this->longitude = $output->results[0]->geometry->location->lng;
}
0 likes
13 replies
LaryAI's avatar
Level 58

It sounds like the issue might be related to how Livewire is binding the input fields to the component properties. When you update the properties in the Livewire component, the input fields should automatically reflect these changes if they are properly bound.

Here's a step-by-step solution to ensure that your input fields for longitude and latitude are correctly updated:

  1. Ensure Proper Binding in Blade Component: Make sure that your input fields are correctly bound to the Livewire component properties using the wire:model directive.

    <div>
        <input type="text" wire:model="longitude" placeholder="Longitude">
        <input type="text" wire:model="latitude" placeholder="Latitude">
    </div>
    
  2. Livewire Component: Ensure that your Livewire component has the necessary properties and listeners set up.

    use Livewire\Component;
    
    class AddressLookup extends Component
    {
        public $longitude;
        public $latitude;
    
        protected $listeners = ['updateAddress'];
    
        public function updateAddress($longitude, $latitude)
        {
            $this->longitude = $longitude;
            $this->latitude = $latitude;
        }
    
        public function render()
        {
            return view('livewire.address-lookup');
        }
    }
    
  3. Dispatching the Event: Ensure that the event is being dispatched correctly from your JavaScript code.

    document.addEventListener('DOMContentLoaded', function () {
        // Assuming you have the logic to get the longitude and latitude
        let longitude = ...;
        let latitude = ...;
    
        Livewire.emit('updateAddress', longitude, latitude);
    });
    
  4. Debugging:

    • Check the browser console for any JavaScript errors.
    • Ensure that the Livewire component is correctly receiving the event and updating the properties.
    • You can add some debugging statements in your Livewire component to verify that the updateAddress method is being called and the properties are being updated.
    public function updateAddress($longitude, $latitude)
    {
        $this->longitude = $longitude;
        $this->latitude = $latitude;
    
        // Debugging
        logger('Longitude: ' . $this->longitude);
        logger('Latitude: ' . $this->latitude);
    }
    

By following these steps, you should be able to ensure that the input fields for longitude and latitude are correctly updated when the updateAddress event is dispatched. If the issue persists, double-check the bindings and event dispatching logic.

tuursw's avatar

@valentin_vranic Hello Valentin, I am using Livewire 3.5

namespace App\Livewire;

use Livewire\Component;

class GeocoordinatesLookup extends Component
{
    public $address = '';
    public $lat = ''; // latitude
    public $lng = ''; // longitude

    protected $listeners = [
        'updateAddress'
    ];

    public function mount(){

        $this->address = 'Steentstraat 23, Brugge, Belgium';
        $this->getCoordinates($this->address);
    }

    public function render()
    {
        return view('livewire.geocoordinates-lookup');
    }

    public function updateAddress($address)
    {
        $this->address = $address;
        $this->getCoordinates($address);
    }

    public function getCoordinates($address)
    {
        if (!$address) return [];

        $prepAddr = str_replace(' ', '+', $address);
        $geocode = file_get_contents('https://maps.google.com/maps/api/geocode/json?address=' . $prepAddr . '&sensor=false&key=' . env('GOOGLE_MAP_KEY'));

        $output = json_decode($geocode);

        $this->lat = $output->results[0]->geometry->location->lat;
        $this->lng = $output->results[0]->geometry->location->lng;
    }
}
valentin_vranic's avatar

@tuursw hmm, and how does your x-input component looks like? Maybe there is the issue, with property drilling

tuursw's avatar

@valentin_vranic I have already tried to replace it by regular input field (without using the component) but that didn't help either.

valentin_vranic's avatar

@tuursw at the end, what do you want to achieve? To get latitude and altitude values when you enter an address? For that to achieve you can set wire:model.blur="address" and instead in mount you can set this $this->getCoordinates($this->address); part into render method. Or set a "get" button, and call a method on it.

Or create updated lifecycle hook for address (still on blur), to make call only when address changes

    public function updatedAddress()
    {
        $this->getCoordinates($this->address);
    }
tuursw's avatar

@valentin_vranic Getting the lat and long when entering the address works fine, it triggers perfectly when you select an address from the google location suggest with that

Livewire.emit('updateAddress', longitude, latitude);

And when I use the value instead of wire.model then the input field is updated correctly.

<x-input type="text" value="longitude"/>

instead of

<x-input type="text" wire:model="longitude"/>

The problem is that property isn't synced by using the livewire model attribute... so if they change the value of the input field, the property isn't updated in the component.

I managed to work around it, but there seems to be something conflicting or a bug somewhere...

valentin_vranic's avatar

@tuursw still not sure why you need this part

Livewire.emit('updateAddress', longitude, latitude);?

When on mount, render you can call the getCoordinates method. You could take into consideration my previous solutions (pure livewire), cuz' I'm running out of ideas.

tuursw's avatar
Level 6

@valentin_vranic This is some code suggested by the autoreply AI bot I guess, not a part of my code

Livewire.emit('updateAddress', longitude, latitude);

I'll try a new fresh installation to dig deeper into this... it's not a problem anymore as I have a workaround now, but it itches to find out what is going wrong ;)

Snapey's avatar

by the way, never use .env outside of config files

file_get_contents('https://maps.google.com/maps/api/geocode/json?address=' . $prepAddr . '&sensor=false&key=' . env('GOOGLE_MAP_KEY'));

You should create an entry, perhaps in services.php that uses the .env file and then use the config entry in your code

file_get_contents('https://maps.google.com/maps/api/geocode/json?address=' . $prepAddr . '&sensor=false&key=' . config('services.google.map_key'));
tuursw's avatar

@Snapey I still need to polish that bit, will be changed for the production environment

jaseofspades88's avatar

Adding the live modifier like this wire:model.live="address" will give you the reactivity on the properties. It's all well documented....

tuursw's avatar

@jaseofspades88 I did try the .live modifier but that didn't seem to work either, the documentation is great indeed, but doesn't always helps ;-)

Please or to participate in this conversation.