How to pass alpinejs value to livewire component in php
Learn how to seamlessly pass Alpine.js values to Livewire components in PHP. This guide covers real-time data binding with @entangle, calling Livewire methods using $wire, and dispatching events for dynamic interactions. Enhance your Laravel apps with smooth Alpine.js and Livewire integration!
Laravel

Efficient state management between Livewire and Alpine.js is crucial for building dynamic, real-time applications in Laravel. While Livewire handles server-side reactivity and Alpine.js provides a lightweight frontend layer, ensuring seamless data synchronization between the two can sometimes be challenging.

In this tutorial, we’ll explore three effective techniques for passing Alpine.js values to Livewire components:

  • @entangle directive for automatic two-way binding.

  • Dispatching browser events to trigger Livewire actions dynamically.

  • Using Livewire’s $wire object for direct method calls and property updates.

Mastering these approaches will help you build more robust, interactive, and maintainable Livewire-Alpine.js integrations. Let’s dive in. 

Pre-requisite

To follow along effectively with this tutorial, the following is required

 

With that said, let's delve right into the tutorial.

 


 

Using @entangle (Best for Two-Way Binding)

The @entangle directive syncs an Alpine.js property with a Livewire component property.

Livewire Component (PHP)

class ExampleComponent extends Component

{

    public $name = '';




    public function render()

    {

        return view('livewire.example-component');

    }

}

Blade View

<div x-data="{ name: @entangle('name') }">

    <input type="text" x-model="name" class="border p-2" placeholder="Enter your name">

    <p>Your name is: <span x-text="name"></span></p>

</div>

 

From the example above, Livewire updates automatically when the value of name changes in Alpine.

 


 

Using $wire Object (Direct Method Call)

You can call Livewire methods directly from Alpine.js.

Blade View

<div x-data="{ message: '' }">

    <input type="text" x-model="message" class="border p-2">

    <button @click="$wire.updateMessage(message)" class="bg-blue-500 text-white p-2">Send</button>

</div>


<livewire:example-component />

Livewire Component (PHP)

class ExampleComponent extends Component

{

    public $message = '';




    public function updateMessage($newMessage)

    {

        $this->message = $newMessage;

    }




    public function render()

    {

        return view('livewire.example-component');

    }

}

Livewire updates when the button is clicked.

 


 

Using Custom Events (dispatch)

You can dispatch an event from Alpine.js and listen for it in Livewire.

Blade View

<div x-data>

    <button @click="$dispatch('update-name', 'Moses')" class="bg-green-500 text-white p-2">Set Name</button>

</div>




<livewire:example-component />

Livewire Component (PHP)

class ExampleComponent extends Component

{

    public $name = '';




    protected $listeners = ['update-name' => 'setName'];




    public function setName($newName)

    {

        $this->name = $newName;

    }




    public function render()

    {

        return view('livewire.example-component');

    }

}

Livewire updates when the button is clicked and dispatches an event.

 

 

Here are some real-world examples of passing Alpine.js values to Livewire components, covering different use cases.

 


 

Real-Time Search with @entangle (Best for Instant Filtering)

Imagine a product search feature where the user types in an input field, and the search results update instantly.

Livewire Component (PHP)

class ProductSearch extends Component

{

    public $search = '';



    public function render()

    {

        return view('livewire.product-search', [

            'products' => Product::where('name', 'like', "%{$this->search}%")->get()

        ]);

    }

}

 

Blade View

<div x-data="{ search: @entangle('search').defer }">

    <input type="text" x-model="search" class="border p-2 w-full" placeholder="Search products...">

    

    <ul>

        @foreach($products as $product)

            <li>{{ $product->name }}</li>

        @endforeach

    </ul>

</div>

 

✅ Typing in the search box updates Livewire’s $search property instantly, triggering a Livewire re-render.

 


 

Updating Livewire from an Alpine Component (Calling $wire)

Imagine a rating system where clicking a star updates the rating in Livewire.

Blade View

<div x-data="{ rating: 0 }">

    <div class="flex">

        <template x-for="star in 5">

            <span @click="rating = star; $wire.updateRating(star)" class="cursor-pointer text-yellow-500">

                ★

            </span>

        </template>

    </div>




    <p>Selected Rating: <span x-text="rating"></span></p>

</div>




<livewire:rating-component />

 

Livewire Component (PHP)

class RatingComponent extends Component

{

    public $rating = 0;




    public function updateRating($newRating)

    {

        $this->rating = $newRating;

    }




    public function render()

    {

        return view('livewire.rating-component');

    }

}

 

Clicking a star updates both Alpine.js and Livewire.

 


 

Toggling Sidebar with Alpine & Livewire (Using Events)

You might have a sidebar menu where Livewire needs to know when it's open/closed.

Blade View

<div x-data="{ open: false }">

    <button @click="open = !open; $dispatch('sidebar-toggle', open)" class="bg-blue-500 text-white p-2">

        Toggle Sidebar

    </button>




    <aside x-show="open" class="fixed left-0 top-0 w-64 h-full bg-gray-800 text-white p-4">

        Sidebar Content

    </aside>

</div>




<livewire:sidebar-component />

 

Livewire Component (PHP)

class SidebarComponent extends Component

{

    public $isOpen = false;




    protected $listeners = ['sidebar-toggle' => 'setOpen'];




    public function setOpen($state)

    {

        $this->isOpen = $state;

    }




    public function render()

    {

        return view('livewire.sidebar-component');

    }

}

Livewire now knows if the sidebar is open or closed.

 


 

File Upload with Alpine.js & Livewire

If you want to preview an image before uploading while also handling the upload with Livewire, you can use Alpine.js.

Blade View

<div x-data="{ preview: null }">

    <input type="file" @change="preview = URL.createObjectURL($event.target.files[0]); $wire.uploadFile($event.target.files[0])">

    

    <img x-show="preview" :src="preview" class="mt-4 w-32 h-32">

</div>




<livewire:file-upload />

 

Livewire Component (PHP)

use Livewire\WithFileUploads;




class FileUpload extends Component

{

    use WithFileUploads;




    public function uploadFile($file)

    {

        $path = $file->store('uploads', 'public');

    }




    public function render()

    {

        return view('livewire.file-upload');

    }

}

Alpine previews the file; Livewire handles the upload.

 


 

Conclusion?

In this tutorial, we walked through the processes of 

  • Use @entangle for real-time syncing (e.g., search fields). 🔄

  • Use $wire for direct Livewire calls (e.g., rating systems). ⚡

  • Use events ($dispatch) for communication between components (e.g., sidebar toggles). 🔔

  • Use Alpine.js for UI previews and Livewire for backend logic (e.g., file uploads). 🎨



Author: moses on 05-02-2025
Related Posts
Built by codecontent.pro in partnership with Laraveldev.pro
© 2025 Laraveldev.pro