Laravel Google Pie Chart Tutorial Example

Laravel 6 , Laravel , Laravel 7



Laravel Google Pie Chart Tutorial Example

Hello Friends,

Today, I am going to learn you how to create google pie chart in laravel application. We will create google pie chart in laravel app. In this blog I will show you create google pie chart in laravel app.

Google has provide us different type of Google chart. So, in this post we are going to create 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 courses.i will show percentage of courses student available,. So Let's follow bellow step by step.

Step 1 : Install Laravel App

In this step, You can install laravel fresh app. So open terminal and put the bellow command.

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

Step 2 : Setup Database Configuration

After successfully install laravel app thenafter 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 step we have to create 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.



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 require 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.



namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model


protected $fillable = ['name','course'];


Step 3 : Create Route

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


Route::get('google-pie-chart', '[email protected]');

Step 4 : Create Controler

Here this step now we should create 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.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class StudentController extends Controller


public function googlePieChart()


$data = DB::table('students')


DB::raw('course as course'),

DB::raw('count(*) as number'))



$array[] = ['Course', 'Number'];

foreach($data as $key => $value)


$array[++$key] = [$value->course, $value->number];


return view('google-pie-chart')->with('course', json_encode($array));



Step 5 : Create Blade File

In last step. In this step we have to create blade 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:


<!DOCTYPE html>



<title>laravel google pie chart tutorial example -</title>

<script src=""></script>

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

<link rel="stylesheet" href="" />

<style type="text/css">



margin:0 auto;





<div class="container" style="margin-top: 50px;">

<div class="row">

<div class="col-md-10 col-md-offset-1">

<div class="panel panel-primary">

<div class="panel-heading">

<h3 class="panel-title">Laravel Google Pie Chart Tutorial Example -</h3>


<div class="panel-body" align="center">

<div id="pie_chart" style="width:750px; height:450px;">







<script type="text/javascript">

var analytics = <?php echo $course; ?>

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


function drawChart()


var data = google.visualization.arrayToDataTable(analytics);

var options = {

title : 'Percentage of Students Courses(BCA,BCOM,BSC)'


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

Recommended Posts