How To Create Dynamic Bar Chart In Laravel

WebSolutionStuff | Jul-01-2020 | Categories : Laravel PHP jQuery

Today i will show you how to create dynamic bar chart in laravel, charts are use to representing data in graphics view, for creation of dynamic bar chart example you need to create route, controller, blade file and database, So if you will follow my tutorial step by step then definitely you will get output. So. let's start.



Step 1: Install Laravel

Type the following command in terminal.

composer create-project --prefer-dist laravel/laravel barchart


 Step 2 : Add/Create Migration Table

We need dynamic data for bar chart example. So first, we have to create migration for "product" table using Laravel php artisan command, so first type below command:

php artisan make:migration create_products_table --create=products

After run this command you will find php file here location "database/migrations/" in this file you need to add below code.


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('product', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

after this we need to run this migration by following command in our terminal:

php artisan migrate


Step 3: Add Resource Route

Now Add  route in Routes/web.php

Route::get('barchart', '[email protected]');


Step 4 : Create Controller And Model

after adding route we need to create new conroller and model so type below command for create controller.

php artisan make:controller BarchartController
php artisan make:model Product


Step 5 : Add Code In Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class BarchartController extends Controller
    public function barchart(Request $request)
    	$fruit = Product::where('product_type','fruit')->get();
    	$veg = Product::where('product_type','vegitable')->get();
    	$grains = Product::where('product_type','grains')->get();
    	$fruit_count = count($fruit);    	
    	$veg_count = count($veg);
    	$grains_count = count($grains);
    	return view('barchart',compact('fruit_count','veg_count','grains_count'));


Step 6: Create Blade File

Now in this final step we are creating barchart.blade.php file for view.

<!DOCTYPE html>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>how to create dynamic barchart in laravel -</title>	
	<link rel="stylesheet" href="">
	<link href="{{asset('assets/css/components.min.css')}}" rel="stylesheet" type="text/css">	
	<script type="text/javascript" src="{{asset('assets/js/jquery.min.js')}}"></script>
	<script type="text/javascript" src="{{asset('assets/js/bootstrap.bundle.min.js')}}"></script>	
	<script type="text/javascript" src="{{asset('assets/js/echarts.min.js')}}"></script>

<div class="col-md-12">
	<h1 class="text-center">how to create dynamic barchart in laravel -</h1>
    <div class="col-md-8 col-md-offset-2">
    	<div class="col-xl-6">
    		<div class="card">
    			<div class="card-body">
    				<div class="chart-container">
    					<div class="chart has-fixed-height" id="bars_basic"></div>

<script type="text/javascript">
var bars_basic_element = document.getElementById('bars_basic');
if (bars_basic_element) {
    var bars_basic = echarts.init(bars_basic_element);
        color: ['#3398DB'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {            
                type: 'shadow'
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        xAxis: [
                type: 'category',
                data: ['Fruit', 'Vegitable','Grains'],
                axisTick: {
                    alignWithLabel: true
        yAxis: [
                type: 'value'
        series: [
                name: 'Total Products',
                type: 'bar',
                barWidth: '20%',
                data: [


So, We are done with our code and you can see output as below screen print.



Clone Project From Github : Dynamic Bar Chart In Laravel


Featured Post

Follow us
facebooklogo github instagram twitter