Livewire withcount is lost

Viewed 201

I have a table where I want to show the data from a collection and the count of an hasmany relationship. I want to use the Laravel withcount way so that I won't load too many models. The query looks like this: Company::withCount('transactions');

Query works fine and adds the transactions_count to the collection.

On the first render it's also shown in de blade. But when Livewire makes a roundtrip that field/data gets lost. I guess it's not being send from Livewire to the backend. Of course I could load all the models and count them in php, but I would rather not.

What can I do that this additional field is not lost?

1 Answers

I found a solution, maybe it's not the perfect one but it should be okay I guess.

You can add a protected $withCount = ['relation'] to your Model, with this Livewire will actually save count property all the time.

If you have come up with a different solution let me know.

Related