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

vincent15000's avatar

Dynamic chart with Livewire / AlpineJS / ChartJS

Hello,

I have followed the Laravel Cookbook two first videos by Andre Madarang and I don't understand why my code doesn't work as expected.

Here is my Livewire class.

And here my Livewire blade view.

When I load the component, it works fine, the chart is correctly displayed with 2025 datas.

Then I select 2024 with the select / options field and nothing happens.

I come back to 2025 and the chart changes with the 2024 datas.

I change the year to 2024 and the chart changes with the 2025 datas.

I thought it was due to entangle and I have tried to add .live to chart: $wire.entangle('chart').live, but it doesn't work better.

I have checked with some dd() in the class, the $chart variable is correcty updated with the right datas. So the problem seems to come from the synchronization of the datas between Livewire and AlpineJS.

What happens ?

Can you help me please ?

Thanks a lot ;).

V

0 likes
2 replies
vincent15000's avatar

Something strange ... I just checked the $selectedYear variable, it isn't updated dynamically on the screen.

<select
    wire:model.live="selectedYear"
    class="transition-all outline-none px-3 py-2 rounded-full bg-gray-300 text-black"
>
    @foreach ($years as $year)
        <option value="{{ $year }}" @selected($year == $selectedYear)>{{ $year }}</option>
    @endforeach
</select>

{{ $selectedYear }} // always 2025

And when I add <span x-text="chart.config.data.datasets[0].data[0]"></span>, I see that the datas are correctly updated, so the problem doesn't come from the synchronization, but probably from Livewire.on => datas are not updated inside Livewire.on (checked with console.log()).

vincent15000's avatar

For the moment, the only way I have found is to pass the new data via the event.

public function updatedSelectedYear()
{
    $this->setChartDatas();

    $this->dispatch('chart-datas-updated', chart: $this->chart);
}

And get the datas in the listener.

x-data="{
    chart: @js($chart),

    init() {
        const chart = new Chart(this.$refs.chart, this.chart.config);

        Livewire.on('chart-datas-updated', (datas) => {
            chart.data.datasets[0].data = datas.chart.config.data.datasets[0].data;

            chart.update();

        });
    },
}"

Any idea why it doesn't work via entangle ?

Please or to participate in this conversation.