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

anonymouse703's avatar

dispatching event in parent component from child is not working..

In my Livewire2 project I can dispatch event by using emit and make listener through $listerner but in the livewire3 I found it hard on dispatching event from child

The create, store and delete is working but on edit/update is not for I cannot dispatch and listen the event I created.

The case is this I created a Index.php for my parent component and Create.php for child component

whole code, in the index i include the create component through modal

<div>
         <!-- Page Header -->
        <div class="block justify-between page-header md:flex">
            <div>
                <h3 class="text-gray-700 hover:text-gray-900 dark:text-white dark:hover:text-white text-2xl font-medium"> Sample</h3>
            </div>
            <button wire:click="$dispatch('open-modal')" class="hs-dropdown-toggle ti-btn ti-btn-primary hover cursor-pointer">
                Add
            </button>
        </div>
        <!-- Page Header Close -->

        <!-- Start::row-1 -->
        <div class="grid grid-cols-12 ">
            <div class="col-span-12 lg:col-span-12">
                <div class="box">
                    <div class="box-header">
                        <h5 class="box-title">Sample Table</h5>
                    </div>
                    <div class="box-body p-0">
                    <div class="overflow-auto">
                        <table class="ti-custom-table ti-custom-table-head ti-striped-table">
                            <thead>
                                <tr>
                                <th scope="col">Name</th>
                                <th scope="col">Address</th>
                                <th scope="col" class="!text-end">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($datas as $data)
                                    <tr>
                                        <td class="font-medium">{{ $data->name }}</td>
                                        <td>{{ $data->address }}</td>
                                        <td class="text-end font-medium flex gap-2">
                                            <button class="text-primary hover:text-primary" wire:click="edit({{ $data->id }})">Edit</button> 

                                            <!-- <button wire:click="$dispatch('edit-sample', {id: {{ $data->id }} })">Test</button> -->

                                             <button class="text-warning hover:text-warning" wire:click="$dispatch('delete', {id: {{ $data->id }} })">Delete</button>

                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                        {{$datas->links()}}
                    </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- End::row-1 -->

        <!-- Main modal -->
       @if($isOpen)
        <div  tabindex="-1" aria-hidden="true" class="hs-overlay ti-modal fixed inset-0 flex items-center justify-center pt-5">
            <div class="relative w-full h-full max-w-2xl md:h-auto">
                <!-- Modal content -->
                <livewire:sample.create>
            </div>
        </div>
        @endif

</div>

Index.php

<?php

namespace App\Livewire\Sample;

use Livewire\Component;
use App\Repositories\Contracts\SampleRepositoryInterface;
use Livewire\Attributes\On;

class Index extends Component
{

    public $isOpen = false;


    #[On('open-modal')]   
    public function openModal()
    {
        $this->isOpen = true;
    }

    #[On('close-modal')]   
    public function closeModal()
    {
         $this->isOpen = false;
    }

    public function edit($id)
    {
        $this->isOpen = true;
        $this->dispatch('edit-sample', id: $id)->to(Create::class);
    }


    #[On('delete')]   
    public function delete(SampleRepositoryInterface $sampleRepository, $id)
    {
        $post = $sampleRepository->find($id);

        if ($post) {
            $post->delete();
        }

        $this->dispatch('livewire.sample.index:refresh');
    }

    public function render(SampleRepositoryInterface $sampleRepository)
    {

        $datas = $sampleRepository->paginated();

        return view('livewire.sample.index', ['datas' => $datas]);
    }
}

and in create.blade

<div>
    <div class="ti-modal-content">
        <div class="ti-modal-header">
            <h3 class="ti-modal-title">
                Sample Information
            </h3>
          <!--   <button type="button" wire:click="close" class="hs-dropdown-toggle ti-modal-clode-btn"
                data-hs-overlay="#hs-basic-modal">
               X
            </button> -->
              <button type="button" wire:click="$dispatch('close-modal')" class="hs-dropdown-toggle ti-modal-clode-btn"
                data-hs-overlay="#hs-basic-modal">
               X
            </button>
        </div>
        <div class="ti-modal-body">
              <div class="col-span-12 lg:col-span-4">
                   <form wire:submit.prevent="save">
                        <div class="md:flex md:items-center mb-6">
                            <div class="md:w-1/3">
                                <label class="ti-form-label4 " for="name">
                                    Name
                                </label>
                            </div>
                            <div class="md:w-2/3">
                                <input class="ti-form-input" wire:model="formData.name" type="text">
                            </div>
                        </div>
                        <div class="md:flex md:items-center mb-6">
                            <div class="md:w-1/3">
                                <label class="ti-form-label4 " for="address">
                                    Address
                                </label>
                            </div>
                            <div class="md:w-2/3">
                                <input class="ti-form-input" wire:model="formData.address" type="text">
                            </div>
                        </div>
                        <div class="ti-modal-footer">
                            <button type="button"
                                class="hs-dropdown-toggle ti-btn ti-border font-medium bg-white text-gray-700 shadow-sm align-middle hover:bg-gray-50 focus:ring-offset-white focus:ring-primary dark:bg-bgdark dark:hover:bg-black/20 dark:border-white/10 dark:text-white/70 dark:hover:text-white dark:focus:ring-offset-white/10"
                                 wire:click="$dispatch('close-modal')">
                                Close
                            </button>
                            <button type="submit" class="ti-btn ti-btn-primary">
                                Save
                            </button>
                        </div>
                    </form>
              </div>
        </div>
    </div>
</div>

Create.php

0 likes
0 replies

Please or to participate in this conversation.