How To Create Dynamic Pie Chart In Laravel 8

Websolutionstuff | Oct-02-2020 | Categories : Laravel jQuery

In this article, we will see how to create a dynamic pie chart in laravel 8. Pie charts are used to represent data in graphics view. For the creation of a dynamic pie chart example, you need to create a model, controller, route, blade file, and database. So, follow step by step then definatly you will get the output of a dynamic pie chart example.

So, let's see the pie chart in laravel 8 and dynamic charts in laravel.

Laravel 8 Dynamic Pie Chart Example

Step 1: Install Laravel

Type the below command in your terminal to create a new project of the dynamic pie chart.

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



 Step 2: Add/Create Migration Table

We need dynamic data for the pie chart example. So first, we have to create migration for the "product" table using the laravel php artisan command.

php artisan make:migration create_products_table --create=products

After running this command you will find the php file "database/migrations/".


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

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

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

After this, we need to run this migration by following the command in our terminal.

php artisan migrate



Step 3: Add Resource Route

Now add a route in the web.php file.



use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');

Route::get('echarts', 'App\Http\Controllers\EchartController@echart');


Step 4: Create Controller And Model

After adding the route, we need to create a new controller and model for the pie chart example. So, type the below command to create a controller.

php artisan make:controller EchartController
php artisan make:model Product


Step 5: Add Code In Model And Controller

Now,  Add the below code in the Product Model file.



namespace App\Models;

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

class Product extends Model
    use HasFactory;

    protected $table = 'products';
    protected $guarded = [];



 Add the below code in the app\Http\Controllers\EchartController.php file.


namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;

class EchartController extends Controller
    public function echart(Request $request)
    	$fruit = Product::where('product_type','fruit')->get();
    	$veg = Product::where('product_type','vegitable')->get();
    	$grains = Product::where('product_type','grains')->get();
    	$fruit_count = count($fruit);    	
    	$veg_count = count($veg);
    	$grains_count = count($grains);
    	return view('echart',compact('fruit_count','veg_count','grains_count'));


Step 6: Create Blade File

Now, we are creating an echart.blade.php file for viewing.

	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>how to create dynamic pie chart in laravel 8 -</title>	
	<link rel="stylesheet" href="">
	<link href="{{asset('assets/css/components.min.css')}}" rel="stylesheet" type="text/css">	
	<script type="text/javascript" src="{{asset('assets/js/jquery.min.js')}}"></script>
	<script type="text/javascript" src="{{asset('assets/js/bootstrap.bundle.min.js')}}"></script>	
	<script type="text/javascript" src="{{asset('assets/js/echarts.min.js')}}"></script>	
<div class="col-md-12">
	<h1 class="text-center">how to create dynamic pie chart in laravel 8 -</h1>
	<div class="col-xl-6" style="margin-top: 30px;">
		<div class="card">
			<div class="card-body">
				<div class="chart-container">
					<div class="chart has-fixed-height" id="pie_basic"></div>
<script type="text/javascript">
var pie_basic_element = document.getElementById('pie_basic');
if (pie_basic_element) {
    var pie_basic = echarts.init(pie_basic_element);
        color: [
        textStyle: {
            fontFamily: 'Roboto, Arial, Verdana, sans-serif',
            fontSize: 13

        title: {
            text: 'Pie Chart Example',
            left: 'center',
            textStyle: {
                fontSize: 17,
                fontWeight: 500
            subtextStyle: {
                fontSize: 12

        tooltip: {
            trigger: 'item',
            backgroundColor: 'rgba(0,0,0,0.75)',
            padding: [10, 15],
            textStyle: {
                fontSize: 13,
                fontFamily: 'Roboto, sans-serif'
            formatter: "{a} <br/>{b}: {c} ({d}%)"

        legend: {
            orient: 'horizontal',
            bottom: '0%',
            left: 'center',                   
            data: ['Fruit', 'Vegitable','Grains'],
            itemHeight: 8,
            itemWidth: 8

        series: [{
            name: 'Product Type',
            type: 'pie',
            radius: '70%',
            center: ['50%', '50%'],
            itemStyle: {
                normal: {
                    borderWidth: 1,
                    borderColor: '#fff'
            data: [
                {value: {{$fruit_count}}, name: 'Fruit'},
                {value: {{$veg_count}}, name: 'Vegitable'},
                {value: {{$grains_count}}, name: 'Grains'}




dynamic pie chart in laravel 8


Download Code From Github: Dynamic Pie Chart In Laravel 8


You might also like:

Recommended Post
Featured Post
How To Remove Column From Table In Laravel 10 Migration
How To Remove Column From Tabl...

In this article, we will see how to remove columns from a table in laravel 10 migration. Here, we will learn about...

Read More


Laravel 9 Order By Query Example
Laravel 9 Order By Query Examp...

In this article, we will see laravel 9 order by query example. how to use order by in laravel 9.The orderBy me...

Read More


Razorpay Payment Gateway Integration in Laravel 10 and VueJS 3
Razorpay Payment Gateway Integ...

In the rapidly advancing world of technology, the combination of Laravel 10 and VueJS 3 is unparalleled and exceptionall...

Read More


Laravel 10 AJAX Form Validation Example
Laravel 10 AJAX Form Validatio...

In this article, we will see how to validate the ajax form in laravel 10. Here we will learn about the laravel 10 a...

Read More