Is it possible to pass eloquent collections as function params?

Hey,

is it possible to add a eloquent collection as param to a function in Livewire?

This is my view:

<x-zondicon-edit-pencil wire:click="viewDetailPage({{ $item }})" />

This is my function

public function viewDetailPage($item)
{
   dd($item);
}

The $item returns everytime a array instead of a collection. I have the same issue when I’m emit a event with parameters.

How can I get this to work?

Hi,
Collection is not a “simple” type and not supported by Livewire, but you can always convert array back to collection by using collect($item).

Thank you for your answer!

Yeah thats a good idea, but so $item is no longer a eloquent collection.:thinking:

I am personally against passing models and list of models to the client and back again except user is really editing all fields of those models (i suspect not).
I would suggest to pass model IDs or anything else that helps to reload those models on a server side.

1 Like

You can pass the Model on parameter type

public function viewDetailPage(Item $item)
{
   dd($item);
}