Bar Chart Integration with Laravel 8 Tutorial Example

Share this Article
Reading Time: 7 minutes

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

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

To learn about Pie Chart integration in Laravel 8, click here.

To learn about Line Graph integration in Laravel 8, click here.

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 migration file.

$ php artisan make:model Student -m

It will create two files –

  • Model – Student.php at /app/Models folder
  • Migration file – 2021_05_04_145928_create_students_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 CreateStudentsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('students', function (Blueprint $table) {
            $table->string("name", 120);

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

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


namespace App\Models;

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

class Student extends Model
    use HasFactory;

    public $timestamps = false;

Run Migration

Next, we need to create tables inside database.

$ php artisan migrate

This command will create tables inside database.

Create Data Seeder

Next, creating a seeder files to seed some dummy data for table.

Run this command to terminal –

$ php artisan make:seeder StudentSeeder

It will create a file StudentSeeder.php at /database/seeders folder.

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


namespace Database\Seeders;

use Illuminate\Database\Seeder;

use DB;

class StudentSeeder extends Seeder
     * Run the database seeds.
     * @return void
    public function run()
        $faker = \Faker\Factory::create();

        for ($loop = 0; $loop < 5; $loop++) {
                "name" => $faker->name(),
                "term1_marks" => $faker->numberBetween(45, 95),
                "term2_marks" => $faker->numberBetween(45, 95),
                "term3_marks" => $faker->numberBetween(45, 95),
                "term4_marks" => $faker->numberBetween(45, 95),
                "remarks" => $faker->randomElement(["Good", "Excellent", "Needs Improvement", "Better", "Poor"])

Run Seeder File

Run this command to terminal.

$ php artisan db:seed --class=StudentSeeder

It will create test data into database table.

Create Controller

Next, we need to create a controller file.

$ php artisan make:controller StudentController

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

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


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller
    public function index()
        $students = Student::all();

        $dataPoints = [];

        foreach ($students as $student) {
            $dataPoints[] = array(
                "name" => $student['name'],
                "data" => [

        return view("bar-graph", [
            "data" => json_encode($dataPoints),
            "terms" => json_encode(array(
                "Term 1",
                "Term 2",
                "Term 3",
                "Term 4",

Create Blade Layout File

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

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

<!DOCTYPE html>
<html lang="en">
  <title>Bar Chart in Laravel 8 - Online Web Tutor</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>

<div class="container">
  <h2 style="text-align:center;">Bar Chart in Laravel 8 - Online Web Tutor</h2>
  <div class="panel panel-primary">
    <div class="panel-heading">Bar Chart in Laravel 8</div>
    <div class="panel-body">
        <div id="bar-chart"></div>

<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>

        Highcharts.chart('bar-chart', {
            chart: {
                type: 'column'
            title: {
                text: 'Student Term Wise Marks'
            xAxis: {
                categories: <?= $terms ?>,
                crosshair: true
            yAxis: {
                min: 0,
                title: {
                    text: 'Marks'
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key} Marks</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{}: </td>' +
                    '<td style="padding:0"><b>{point.y}</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
            series: <?= $data ?>


Create Route

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

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


Route::get('bar-graph', [StudentController::class, 'index']);

Application Testing

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

$ php artisan serve


We hope this article helped you to learn Bar Chart Integration with 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

Leave a Comment