DEV Community

Discussion on: Use Laravel charts in Laravel.

 
sammymwangangi profile image
Sammy Mwangangi

I have noticed something with the "undefined variable $chart" error. It was caused when I moved the main layout file to the components folder. That is when I started to implement the Laravel's components feature.
I have created a similar file and placed it on the layouts folder and changed my view file to use the initial methods; @extends and @yield().

Thread Thread
 
arielmejiadev profile image
Ariel Mejia

Hi, you are using the new Laravel components? in this case if you are using variables you need to add a class to add the component logic, look at "app/Views" folder, you can create a view composition class with the artisan command: "php artisan make:component yourcomponent", then it will add a file with a render function that calls directly the view then in the constructor add the $chart variable that you need maybe something like:

php artisan make:component EarningsChart

go to app/Views/chart.php

<?php
namespace App\View\Components;

use Illuminate\View\Component;
use ArielMejiaDev\LarapexCharts\LarapexChart;

class Alert extends Component
{

    /**
     * Chart instance.
     *
     */
    public $chart;

    /**
     * Create the component instance.
     *
     * @param $chart
     * @return void
     */
    public function __construct(LarapexChart $chart)
    {
        $this->chart = (new LarapexChart)
        ->setType('line')
        ->setTitle('Earnings')
        ->setXAxis([
            'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
        ])
        ->setDataset([
            [
                'name'  =>  'Earnings',
                'data'  =>  [0, 10000, 15000, 10000, 20000, 15000, 25000, 20000, 30000, 25000, 40000]
            ]
        ])
    ;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        return view('components.alert');
    }
}

Please let me know if this approach works, and congrats your Tall dashboard looks amazing, I am doing a secret open source project with similar aproach but I prefer VueJS

Thread Thread
 
sammymwangangi profile image
Sammy Mwangangi

Thanks for your feedback. I will try out this approach and I'll let you know if it works. Also, I can't wait to see your project.

Thread Thread
 
arielmejiadev profile image
Ariel Mejia

Thanks!

Thread Thread
 
alexander0312 profile image
alexander lim

hi,, i'm using larapexchart in laravel but i keep on getting a error .. saying that Call to a member function container() on array.. can you help me please...

Thread Thread
 
arielmejiadev profile image
Ariel Mejia

Hi there! thanks for using Larapex charts, here is a live sample with TailwindCSS: larapex-chart-example.herokuapp.com/

Here the controller where I built the charts: github.com/ArielMejiaDev/Larapex-C...

Here the view: github.com/ArielMejiaDev/Larapex-C...

If you want to share the code I can give a look at the code, alternatively you can make a "dd()" to the chart variable before pass it to the view and check that its a LarapexChart object instance.