ArielMejiaDev / larapex-charts

A Laravel wrapper for apex charts
https://larapex-charts.netlify.app/
MIT License
284 stars 84 forks source link

Changes to work in Laravel 10-11 Inertia #105

Closed Altffenser closed 2 months ago

Altffenser commented 5 months ago

I had to make some adjustments to make it work correctly, since this package stopped working after Laravel version 10.

Understanding Inertia's Shared Data concept, it is no longer possible to access the chart.x variable from the definition. And that is precisely the error presented in my case with Laravel 10, intertia and Vue 3.

The solution: In any of our components (Remember, the <script> tag must specify the setupproperty).

import {usePage} from "@inertiajs/vue3";
const props = {
    chart: usePage().props.chart
}
<apexchart :width="props.chart.width" :height="props.chart.height" :type="props.chart.type" :options="props.chart.options"
                           :series="props.chart.series"></apexchart>

That's it, this should work again.

darkons commented 4 months ago

What does this have to do with the Larapex charts package? There is no problem, it all depends on your implementation. I'm using the latest version with Laravel 10 + Inertia and I have no problems.

class DashboardController extends Controller
{
    public function __invoke(AverageChart $chart): Response
    {
        return Inertia::render('Dashboard', [
            'averageChart' => fn () => $chart->build(),
        ]);
    }
}
<script setup>
import AppLayout from '@/Layouts/AppLayout.vue'

defineProps({
  averageChart: Object,
})
</script>

<template>
  <AppLayout title="Dashboard">
    <div class="py-6">
      <div class="mx-auto sm:px-6 lg:px-8">
          <apexchart
            :width="averageChart.width"
            :height="averageChart.height"
            :type="averageChart.type"
            :options="averageChart.options"
            :series="averageChart.series"
          ></apexchart>
      </div>
    </div>
  </AppLayout>
</template>

Your problem seems to be more related to Inertia updates like this one https://inertiajs.com/upgrade-guide#simplified-use-page

ArielMejiaDev commented 2 months ago

As @darkons said there are no issues to use the package with inertia, at this moment you can find even examples in the docs