Skip to content
Advertisement

Passing dynamically data via JS to a chart in a Laravel/Livewire project

i’m developing a laravel 8 + livewire application, and in my app I need to dynamically populate charts. When I insert the charts with static data in my view it is populated and shown correctly, but when I try to populate it dynamically it doesn’t show me anything. To pass the data to the script, from my livewire controller I used the dispatchBrowserEvent method and in the view I listened to the event. But by doing so I don’t get anything and I don’t understand why … do you have any suggestions or ideas?

this is my code:

  • livewire component: TableReportResiduiFasce.php
JavaScript
  • my view: table-report-residui-fasce.blade.php
JavaScript

Advertisement

Answer

You can use Laravel’s {{ }} since this JS is inside <script> tags its code will be pre-processed by Laravel blade engine, therefore every {{ }} will be recognized and substituted before sending the page to the client.

So in your specific case you can just do something like this:

JavaScript

Make sure to also check for 0 values since in these cases you don’t want to show the respective label.

Let’s say for example fascia 5000 - 10000 has a value of 0 then you don’t want to show that label otherwise the graph would make no sense.

Also i have a doubt regarding why you are saving the variables $fascia as arrays, this way you will have to access them like you are doing e.g $fascia6[0]['residuo']. Wouldn’t it be easier to avoid the toArray() method and just saving them as they come back from Eloquent (since they are not a collection given your query). That way you can access them just like $fascia6['residuo']

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement