Google Line Chart Integration in Laravel 8 Tutorial

Share this Article
Reading Time: 6 minutes

Inside this article, we will see the concept of Google Line Chart Integration in Laravel 8. This article will be step by step graph integration.

Line chart represents the information in very graphical view which provides the complete idea about data. We will use Google charts to add Line chart into Laravel 8 application.

Learn More –

Let’s get started.

Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server:

Assuming laravel already installed at system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.


Create Model & Migration

Open project into terminal and run this command to create model & migration file.

$ php artisan make:model Visitor -m

It will create two files –

  • Model – Visitor.php at /app/Models folder
  • Migration file – 2021_07_25_123805_create_visitors_table.php at /database/migrations folder.

Open Migration file and write this complete code into it.


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

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

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

Open model file Visitor.php and write this complete code into it.


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Visitor extends Model
    use HasFactory;

Run Migration

Next, we need to create tables inside database.

$ php artisan migrate

This command will create tables inside database.

Dummy Data for Application

Here, you need to copy the given mysql query and run into sql tab of phpmyadmin.

-- Dumping data for table `visitors`

INSERT INTO `visitors` (`id`, `click`, `viewer`, `created_at`, `updated_at`) VALUES
(1, 25, 80, '2018-07-07 18:30:00', '2018-07-07 18:30:00'),
(2, 10, 45, '2019-07-11 18:30:00', '2019-07-11 18:30:00'),
(3, 18, 65, '2020-07-13 18:30:00', '2020-07-13 18:30:00'),
(4, 14, 57, '2021-07-21 18:30:00', '2021-07-21 18:30:00');

You will see something like this after this test data insertion.

Create Controller

Next, we need to create a controller file.

$ php artisan make:controller SampleController

It will create a file SampleController.php at /app/Http/Controllers folder.

Open SampleController.php and write this complete code into it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\Visitor;

class SampleController extends Controller
    public function googleLineChart()
        $visitor = Visitor::select(
            DB::raw("year(created_at) as year"),
            DB::raw("SUM(click) as total_click"),
            DB::raw("SUM(viewer) as total_viewer")

        $result[] = ['Year', 'Click', 'Viewer'];
        foreach ($visitor as $key => $value) {
            $result[++$key] = ["$value->year", (int)$value->total_click, (int)$value->total_viewer];

        return view('google-line-graph')
            ->with('visitor', json_encode($result));

Create Blade Layout File

Go to /resources/views folder and create a file with name google-line-graph.blade.php

Open google-line-graph.blade.php and write this complete code into it.

    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
      var visitor = <?php echo $visitor; ?>;
      google.charts.load('current', {'packages':['corechart']});

      function drawChart() {
        var data = google.visualization.arrayToDataTable(visitor);
        var options = {
          title: 'Site Visitor Line Chart',
          curveType: 'function',
          legend: { position: 'bottom' }
        var chart = new google.visualization.LineChart(document.getElementById('linechart'));
        chart.draw(data, options);

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

Create Route

Open web.php from /routes folder and add this route into it.

# Add this to header
use App\Http\Controllers\SampleController;


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

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve


We hope this article helped you to Google Line Chart Integration in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4