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

SigalZ's avatar

Livewire emit with multiple parameters - error

I am trying to emit an event that gets 2 parameters.

I get the error:

Unable to resolve dependency [Parameter #1 [ $id ]] in class App\Http\Livewire\MyClass

My component event code:

 protected $listeners = ['deleteSupplier'];

...
public function deleteSupplier($key, $id)
    {
        dd($key); //trying to debug what is going on 
    }

For testing I defaulted the $id parameter and then the dd($key) gave me an array:

//this gives:
array:2 [▼
  0 => 0
  1 => 9

My blade:

@foreach($beanSuppliers as $key => $sup)
<p><a href="#"  wire:click="$emit('deleteSupplierTriggered', {{ $key }}, {{ $sup['id'] }}, '{{ $sup['name'] }}')">
       <i class="fas fa-trash-alt" aria-hidden="true"></i>
 </a></p>
@endforeach

Inspecting a line in firebug shows:

wire:click="$emit('deleteSupplierTriggered', 0, 9, 'Seven Oaks')"

which is correct.

<script>
    Livewire.on("deleteSupplierTriggered", (key, id, removeName) => {
//  for testing I tried: alert(id) it gives the right value
        Swal.fire({
            title: 'Confirm Remove',
            text: `Are you sure you want to remove: ${removeName}`,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, remove it!'
        }).then(result => {
            if (result.value) {
                Livewire.emit("deleteSupplier", [key, id]);
            }
        });
    });
</script>

What am I doing wrong?

0 likes
8 replies
Snapey's avatar

if you are firing an event, then the function takes only two parameters, the event name and the data. If your data contains more than one value then it should be an array

wire:click="$emit('deleteSupplierTriggered',['key' => {{ $key }}, 'id'=> {{ $sup['id'] }}])"

but if this to the same component, just call the method directly, where you can just list the parameters

SigalZ's avatar

@Snapey thank you, but that's not it, that line is calling javascript, and the parameters are being sent fine.

wire:click="$emit('deleteSupplierTriggered', {{ $key }}, {{ $sup['id'] }}, '{{ $sup['name'] }}')">

From my question:

<script>
 Livewire.on("deleteSupplierTriggered", (key, id, removeName) => {
//  for testing I tried: alert(id) it gives the right value

If I try to change the call to the livewire event in the javascript: from:

if (result.value) {
                Livewire.emit("deleteSupplier", [key, id]);

to:

if (result.value) {
                Livewire.emit("deleteSupplier", ['key'=>key, 'id'=>id]);
            }

I get this error in firebug:

Uncaught SyntaxError: invalid arrow-function arguments (parentheses around the arrow-function may help)

Snapey's avatar

i changed my answer because i was forgetting javascript does not have associative arrays

wire:click="$emit('deleteSupplierTriggered',[{{ $key }}, {{ $sup['id'] }}])"

do you really need 2 (or three) parameters in your event?

SigalZ's avatar

@Snapey yes I did. I don't think you follow what I posted in my question and in my reply to you. But as always, thank you for trying to help.

I found the solution, see my next reply.

SigalZ's avatar

Oh, I just saw now you changed your answer, we just kept missing each other responses.

SigalZ's avatar

Ok, I found the problem.

If anyone else struggles, I post the solution here:

In my javascript:

var arg = {'key': key, 'id': id};
            if (result.value) {
                Livewire.emit("deleteSupplier", arg);
            }

In my component's method:

public function deleteSupplier($data)
    {
        $id = $data['id'];
        $key = $data['key'];
}
3 likes

Please or to participate in this conversation.