An image

Corporate Blogs

How To Generate Charts And Graphs Using Laravel 5.6

E-Commerce Businesses Need a Mobile App

Using charts and graphs to represent the data is the best effective solution for any website especially when the site has large data.  The idea behind this blog is to help you guys to generate charts and graphs using Laravel.

Let’s get started!

Install composer

First, we need to install composer, Composer is a package management tool for PHP. Laravel requires a composer for installation.

You can download the composer by using this link. After the installation, you can use “composer” command to check whether the composer got installed or not.

Install Laravel

In this blog, we will be using Laravel 5.6. Alongside, we can also install the Laravel package in three different ways. From command prompt or using terminal or using GitHub
Execute this command to install Laravel, composer global require “laravel/installer” and then create a project like below.

or
we can also create a project using via composer,

Directly clone from GitHub

From this link, you can get the Laravel application and after that, we need to install the composer by using the command composer install.

How to Set Up Laravel On Local Server

Run the below command in command prompt or terminal.

After executing this command Laravel will start working on a local server.
On localhost port, if you want to change the default port number instead of 8000 and you can use the below command to do it. http://localhost:8000

Generating Charts And Graphs

Here we are using consoletvs package for generating charts. So for installation, first, we should change the path for our project using command prompt or terminal. Follow the below steps to install

Step 1

First, we need to install ConsoleTVs/Charts composer package inside our Laravel project. composer require consoletvs/charts.

Step 2

After installing successfully, open app/config.php and add service provider.

Step 3

We need to configure the database for an application. We can configure it in either .env file or config/database.php file.

Step 4

Now we should migrate the default table named “user”. We can find the table in database/migration folder.

Step 5

In order to generate charts first, we must generate dummy records for the demo in the user’s table. For creating dummy records, we need to run the below command in command prompt or terminal.

The above command will create a set of 20 records. If we need to add more records, we can increase the count as much as we want. For example,

Step 6

Creating  Controller – You need to run the below common in the terminal or Command prompt.

Step 7

Adding the routes

We can also add the routes for navigating our application. You can find routes file inside the routes folder. Before 5.4 we can directly find the routes.php file itself, now its web.php. If you are using Laravel 5.2 then you could find the routes.php file inside the app/HTTP folder.

So inside web.php, we can give the path,

Here type will be the parameter we are passing it which will focus on makeChart() function inside chartcontroller.

Related: How To Build REST API with Laravel 5.6

Step 8

Import charts to the controller, On the namespace section add the below line,

Step 9

To create the charts add the below code into chartController, here I have created bar chart, pie, donut, line, area, geo and if you want it more then similarly you can add as much as you want.

Also Read: Tips To Improve The Performance Of A Laravel Application

Step 10

Create a blade file. The blade is the view file used inside the Laravel. You can add a new blade file with any name with an extension of .blade.php. Now, create a blade file and upload this code.

Creating chart.blade.php

Step 11

Use the PHPartisan serve command to run the Laravel application in the local server.

In the above example, we were creating a line chart, geo chart, bar chart, pie chart, donut chart, line chart, and area chart. We can also create a gauge chart, progress bar chart, areaspline chart, scatter chart, percentage chart with help of consoletvs charts composer package.

Also, there are a lot of jquery libraries also available which can help us to create other charts like amcharts, charts, highchairs, google, material, chartist, fusion charts, Morris, etc. Using this plugin, we can easily create all these charts and hopefully, we don’t need any jquery to run this plugin which could be an added advantage of this plugin.

Find this tutorial useful? Share it with your folks who deserve this piece of information. Don’t forget to subscribe to the weekly newsletter for regular updates and tutorials on Laravel development.

Searching for the right technology to start your project? Hire Laravel Developers from Agira Technologies and get started with the project on guaranteed success.  Consult our technology experts today!

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
Professional Life of an IT Consultant Interview with Valentin Crettaz
Written by
Around 3 years of experience he scored intense knowledge on PHP, Laravel, Symfony, Angular & ensuring to learn the new & best methods to bring excellence in building applications. Also he has some aspiring dream to do Doctorate and the strange reason behind this will definitely leave a smile on your face! Yeah because he just loves writing his name as Dr. Harikrishnan

Thoughts on How To Generate Charts And Graphs Using Laravel 5.6



Hi Ulap! Thanks for your interest! Please check whether you have added app.php providers and aliases section inside the config folder, if already added try to use ‘use Charts;’ instead of using ‘ConsoleTVs\Charts\Facades\Charts’

Hi, I followed all the instructions you have stated above, however, I’m getting an error, saying, “Class ‘ConsoleTVs\Charts\Facades\Charts’ not found” .
Do you have any idea on how to fix this issue?

Leave a Comment


You have an Idea. We have the Solution.

We help business evolve with lates technologies and infrastructures tailored to their needs and market trends.