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

tailender's avatar

Passins data between livewire components

Hi,

I have 2 livewire components (using livewire 3) lets say comA and comB. In comA I store data "idNumber" into session

Session::put('id_number', $this->id_number); using Illuminate\Support\Facades\Session;

but I can not retrieve this data in comB from session. But if I store same data in cache then I can retrieve it which I believe is not a good approach. is there any way I can pass data into comB. there is no child component here just 2 separate livewire components.

0 likes
5 replies
LaryAI's avatar
Level 58

To pass data between two separate Livewire components in Livewire 3, you can use events. This approach is more efficient and cleaner than using sessions or cache for such purposes. Here's how you can achieve this:

  1. Emit an event from comA with the data you want to pass.
  2. Listen for that event in comB and handle the data accordingly.

Step-by-Step Solution

1. Emit an Event from comA

In your comA component, you can emit an event with the data you want to pass:

// comA.php
namespace App\Http\Livewire;

use Livewire\Component;

class ComA extends Component
{
    public $id_number;

    public function storeData()
    {
        // Emit an event with the data
        $this->emit('dataPassed', $this->id_number);
    }

    public function render()
    {
        return view('livewire.com-a');
    }
}

2. Listen for the Event in comB

In your comB component, you can listen for the event and handle the data:

// comB.php
namespace App\Http\Livewire;

use Livewire\Component;

class ComB extends Component
{
    public $id_number;

    protected $listeners = ['dataPassed' => 'handleData'];

    public function handleData($id_number)
    {
        $this->id_number = $id_number;
    }

    public function render()
    {
        return view('livewire.com-b');
    }
}

3. Update the Blade Views

Make sure your Blade views for comA and comB are set up correctly to trigger the event and display the data.

<!-- resources/views/livewire/com-a.blade.php -->
<div>
    <input type="text" wire:model="id_number">
    <button wire:click="storeData">Store Data</button>
</div>
<!-- resources/views/livewire/com-b.blade.php -->
<div>
    <p>Received ID Number: {{ $this->id_number }}</p>
</div>

Explanation

  • Emitting the Event: In comA, when the storeData method is called, it emits an event named dataPassed with the id_number as the payload.
  • Listening for the Event: In comB, the $listeners property is used to listen for the dataPassed event. When the event is received, the handleData method is called with the id_number as the parameter, and it sets the id_number property in comB.

This way, you can pass data between two separate Livewire components without using sessions or cache.

tailender's avatar

I haven't been able to pass data through this method. I have tried livewire documentation for events' syntax and functions but not successful in passing data to my component comB which renders when the next page loads.

Snapey's avatar

Assuming both components load on the same request cycle, then the session data is not stored until the end of the request. This means when comA checks the session, its probably not saved yet.

Larys id wont work for similar reasons. Whichever way around, ComB might not yet be mounted when the event is fired.

I would consider if its possible to generate the id somewhere else and pass it as a prop into both components

1 like
tailender's avatar

@Snapey I don't think that is possible as Id will be from user's input in a livewire component.

tailender's avatar

Apparently by adding these classes to Kernel.php middleware array has resolved the issue.

\Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,

Please or to participate in this conversation.