Laravel Google Pie Chart Example

Jun 18, 2021 . Admin

Hi Artisan,

Today, In this article I am going to share you how to integrate google pie chart in our laravel application. i will create google pie chart in laravel app. In this blog I will show you engender google pie chart in laravel project.

So, google we provide us different different type of Google chart. like line chart, bar chart, pie charts this post we are going to engender google pie chart by using Google Chart API in Laravel application.

Here, I will give you full example for laravel google pie chart tutorial example, I am using students table in different different cities.i will show you percentage of student available in cities.So, Let's follow bellow step by step.

Step 1 : Install Laravel App

In this step, we can install laravel fresh application. So open your terminal and put the bellow command.

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Setup Database Configuration

In this step after successfully install laravel app configure databse setup. We will open ".env" file and change the database name, username and password in the env file.

Step 3 : Create Table Migration and Model

In this third step we have to engender migration for students table and Student Model using Laravel php artisan command, so first fire bellow command:

php artisan make:model Student -m

After this command you have to put bellow code in your migration file for create students table.

Path : /database/migrations/2021_06_18_074638_create_students_table.php

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) {

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

Now we need to run migration be bellow command:

php artisan migrate

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

After you have to put bellow code in your Student model file for create students table.

Path : app/Models/Student.php

namespace App\Models;

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

class Student extends Model
    use HasFactory;

    protected $fillable = ['student_name','city'];

Step 3 : Create Route

now, we require to integrate for StudentController in laravel application. so open your "routes/web.php" file and add following route.

Path : routes/web.php
use App\Http\Controllers\StudentController;
use Illuminate\Support\Facades\Route;

| 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('pie-chart', [StudentController::class, 'pieChart']);
Step 4 : Create Controler

Here in this fourth step we should engender new controller as StudentController. So run bellow command and create new controller.

php artisan make:controller StudentController

successfully run above command then,you can create method for get courses and fetch record students table. So Let's copy bellow and put in the controller file.

Path : app/http/controller/StudentController.php

namespace App\Http\Controllers;

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

class StudentController extends Controller
     * Write Your Code..a
     * @return string
    public function pieChart()
        $studentaData = Student::select(
                    DB::raw('city as city'),
                    DB::raw('count(*) as number'))

        $result[] = ['City','Number'];

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

            $result[++$key] = [$value->city, $value->number];


        return view('pieChart')->with('city', json_encode($result));


Step 5 : Create Blade File

In this step we have to engender pieChart.blade.php file. So mainly we have to create google pie chart view file for show pie chart. So finally you have to create following file and put bellow code:

Path : /resources/views/googlePieChart.blade.php
<!DOCTYPE html>
<html lang="en">

    <title>Laravel Google Pie Chart Tutorial Example -</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="">
    <script src=""></script>
    <style type="text/css">
            background: #f7fcff;
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-8 offset-2">
                <div class="card">
                    <div class="card-header bg-secondary text-white">
                        <h4>Laravel Google Pie Chart Tutorial Example -</h4>
                    <div class="card-body">
                        <div id="pie_chart"></div>

    <script type="text/javascript" src=""></script>
        var analytics = <?php echo $city; ?>

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

        function drawChart() {
            var data = google.visualization.arrayToDataTable(analytics);
            var options = {
                title : 'Students List In Different Different Cities.',
                pieSliceText: 'label',
                legend: { position: 'bottom' },

            var chart = new google.visualization.PieChart(document.getElementById('pie_chart'));
            chart.draw(data, options);




Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:


It will help you...

#Laravel 8 #Laravel