Laravel Google Bar Chart Example Tutorial

By Hardik Savani November 5, 2023 Category : Laravel


In this quick example, let's see laravel google bar chart example. you will learn how to use google bar chart in laravel. This article will give you simple example of google bar chart in laravel. you can see laravel google bar chart tutorial.

we can simply create google bar chart in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version application.

In this example, we will create a visitors table with a date, click, and views column. we will add dummy records every day. Then we fetch and display it in a google bar chart. So you can follow the below step and learn how to use the google bar chart in laravel.


Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Migration and Model

we require to create new table "visitors" that way we will get data from this table, you can use your own table but this is for example. we have to create migration for visitors table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_visitors_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create visitors table.


use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration



* Run the migrations.


* @return void


public function up()


Schema::create('visitors', function (Blueprint $table) {









* Reverse the migrations.


* @return void


public function down()





Ok, now you can add few records like as bellow:

Let's create model using bellow command:

php artisan make:model Visitor



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Visitor extends Model


use HasFactory;


Step 3: Add Route

In this is step we need to add route for generate view. so open your route file and add following route.



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\GoogleController;



| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!



Route::get('google-chart', [GoogleController::class, 'googleLineChart']);

Step 4: Create Controller

If you haven't GoogleController then we should create new controller as GoogleController in this path app/Http/Controllers/GoogleController.php. Make sure you should have visitor table with some data. this controller will manage data and chart data and view file, so put bellow content in controller file:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Visitor;

class GoogleController extends Controller



* Write code on Method


* @return response()


public function googleLineChart()


$visitors = Visitor::select("visit_date", "click", "viewer")->get();

$result[] = ['Dates','Clicks','Viewers'];

foreach ($visitors as $key => $value) {

$result[++$key] = [$value->visit_date, (int)$value->click, (int)$value->viewer];


return view('googleChart', compact('result'));



Step 5: Create View File

In last step, we have to create view file "googleChart.blade.php" for generate view chart, so create googleChart file and put bellow code:




<script type="text/javascript" src=""></script>

<script type="text/javascript">

google.charts.load('current', {'packages':['bar']});


function drawChart() {

var data = google.visualization.arrayToDataTable({{ Js::from($result) }});

var options = {

chart: {

title: 'Website Performance',

subtitle: 'Click and Views',



var chart = new google.charts.Bar(document.getElementById('barchart_material'));

chart.draw(data, google.charts.Bar.convertOptions(options));





<h1>Google Bar Chart Example -</h1>

<div id="barchart_material" style="width: 900px; height: 500px;"></div>



Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output: