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

anonymouse703's avatar

What is the proper way for editing data in modal?

I'm using SPA style in list and form (modal), I already have index, create and remove function these are all working now the problem I face now is on edit.

  1. How to pass the id on the event
  2. How to fetch id from the listener

or Do you have any suggestion on proper way on editing and updating data in a modal

these are my initial setup

index.blade.php

    <button class="text-primary hover:text-primary" wire:click="editSample({{ $data->id }})">Edit</button

in index.php

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

and in the create.php

<?php

namespace App\Livewire\Sample;

use Livewire\Component;
use Livewire\Attributes\On;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\Admin\Sample\StoreRequest;
use App\Repositories\Contracts\SampleRepositoryInterface;

class Create extends Component
{

    public $formData = [
        'id' => '',
        'name' => '',
        'address' => ''
    ];

    public function close()
    {
        $this->dispatch('close-modal');
    }


    public function save()
    {
        $sampleRepository = app(SampleRepositoryInterface::class);

        $validatedData = Validator::make($this->formData, (new StoreRequest)->rules())->validate();

        $sampleRepository->create($validatedData);

        return redirect()->to('/sample')->with('status', 'Post created!');
    }

  
    #[On('edit-sample')]          
    public function edit(int $id) 
    {
        $sampleRepository = app(SampleRepositoryInterface::class);


        $sample = $sampleRepository->find($id);

        $this->formData = [
            'id' => $sample->id,
            'name' => $sample->name,
            'address' => $sample->address,
          ];

        $this->dispatch('open-modal');
    }

    public function render()
    {
        return view('livewire.sample.create');
    }

}
0 likes
6 replies
martinbean's avatar

@anonymouse703 Personally, I don’t, as it’s terrible UX. Modals should just be used for messages and alerts; not for “pages” like edit forms.

1 like
anonymouse703's avatar

@jlrdw I put the form in the modal, I was able to store data from the modal the problem is on edit/update side.. in livewire2 I was able to work it perfectly but in the livewire v3 it can't be applied.

Snapey's avatar

Does your index component receive the wire:ckick?

how are your fields bound to the form elements?

anonymouse703's avatar

@Snapey yes sir, when I dump the id from the wire click I get some data, the only thing is the event I dispatch from index.php to create.php is not working (parent to child) but on the close function on the create.php where I dispatch a event it's working (child to parent component)

Scenario: dispatching close event from create.php (child to parent component)

    public function close()
    {
        $this->dispatch('close-modal');
    }

and in the index.php

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

The above code is working, but when I tried to edit the data I can trigger the wire click and dd the id but can't trigger the edit function on the create.php

index.php (if I dd the event)

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

the response is this

Livewire\Features\SupportEvents\Event {#1536 ▼ // app/Livewire/Sample/Index.php:30
  #name: "edit-sample"
  #params: array:1 [▼
    "id" => 2
  ]
  #self: null
  #component: null
}

and I remove the dd and test the dd in the create.php there is no interaction

 #[On('edit-sample')]
    public function edit($id)
    {
        dd($id);

        $this->isEdit = true;

        $sampleRepository = app(SampleRepositoryInterface::class);
        $sample = $sampleRepository->find($id);

        $this->formData = [
            'id' => $sample->id,
            'name' => $sample->name,
            'address' => $sample->address,
        ];

        $this->dispatch('open-modal');
    }

Please or to participate in this conversation.