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

robben07's avatar

Issue with Livewire Postcode Autocomplete

I’m implementing a postcode autocomplete feature using Livewire. The functionality is designed so that when users enter more than three characters into a textbox, a list of postcode suggestions should be displayed for them to select.

I’m encountering an issue where the updatedPostcode() method doesn’t seem to be called when a partial postcode is entered. I’ve added logging within the method, but nothing is being outputted in the logs, which makes it difficult to debug.

Additionally, I’d like to clarify the proper way to import Livewire scripts. Should I use @livewireScriptConfig or @livewireScripts? When I use @livewireScripts, I receive a console warning stating, “Detected multiple instances of Alpine running.”

Here are the relevant files for context:

\resources\views\components\layouts\app.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>FuelRadar</title>
    @vite(['resources/css/app.css'])
    @livewireStyles
</head>
<body class="bg-gray-100">
    <div class="container mx-auto px-4 py-8">
       {{ $slot }}
    </div>

    @livewireScriptConfig
    @vite(['resources/js/app.js'])
</body>
</html>

\app\Livewire\SearchForm.php

\resources\views\livewire\search-form.blade.php (I’ve taken some of the code out)

<div>
    <h1 class="text-2xl font-bold mb-4">Test</h1>

    <p class="mb-4">Enter your postcode.</p>

    <div class="flex items-center mb-4" style="gap: 1rem;">
        <input
            type="text"
            wire:model.debounce.300ms="postcode"
            placeholder="Enter postcode"
            class="w-full border border-gray-300 rounded-md p-2 pr-10"
        >
    </div>

    <!-- Test: Message when more than 3 characters are typed -->
    @if (strlen($postcode) > 3)
        <div class="mt-2 text-green-500">You have entered more than 3 characters!</div>
    @endif
</div>

I'm new to Livewire so I've probably missed something simple.

Thank you for your help.

0 likes
4 replies
LaryAI's avatar
Level 58

To address the issues you're facing with the Livewire postcode autocomplete feature, let's break down the solution into two parts: ensuring the updatedPostcode() method is called correctly and resolving the Alpine.js warning.

1. Ensuring updatedPostcode() is Called

The updatedPostcode() method should be triggered when the postcode property is updated. Here are a few things to check:

  • Debounce Time: You are using wire:model.debounce.300ms="postcode", which is correct for adding a delay before the method is triggered. Ensure that the input field is correctly bound to the postcode property.

  • Logging: Since you mentioned that logging isn't working, ensure that your logging configuration is set up correctly in config/logging.php. You can also try a simple dd() (dump and die) in the updatedPostcode() method to see if it's being called.

  • Livewire Component Registration: Ensure that your Livewire component is registered correctly. You should have something like this in your web.php routes file or wherever you register Livewire components:

    use App\Livewire\SearchForm;
    
    Route::get('/search', SearchForm::class);
    

2. Resolving the Alpine.js Warning

The warning about multiple instances of Alpine.js is likely due to how scripts are being included. Here's how you can address it:

  • Script Inclusion: Use @livewireScripts instead of @livewireScriptConfig. The @livewireScriptConfig is not a standard directive and might be causing issues. Ensure you have the following in your app.blade.php:

    @livewireStyles
    ...
    @livewireScripts
    
  • Alpine.js: If you're using Alpine.js separately, ensure it's not being included twice. Livewire includes Alpine.js by default, so you might not need to include it again in your app.js or elsewhere.

Additional Debugging Tips

  • Console Errors: Check the browser console for any JavaScript errors that might be preventing Livewire from functioning correctly.

  • Network Requests: Use the browser's network tab to ensure that requests to the API are being made when you type in the input field.

  • Livewire Version: Ensure you are using a compatible version of Livewire with your Laravel application.

By following these steps, you should be able to resolve the issues with your Livewire postcode autocomplete feature. If problems persist, consider creating a minimal reproducible example to isolate the issue further.

robben07's avatar

When I run npm run build: I'm getting the following error: [vite]: Rollup failed to resolve import "livewire" from "C:\Users\rob\Herd\MyApp\resources\js\app.js". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to build.rollupOptions.external error during build: Error: [vite]: Rollup failed to resolve import "livewire" from "C:\Users\rob\Herd\MyApp\resources\js\app.js". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to

robben07's avatar

This is the contents of the app.js file

robben07's avatar

I've got it working, you need to use wire:model.live.debounce instead of wire:model.debounce.

Please or to participate in this conversation.