In this tutorial you will learn about the Laravel 7 Google Bar Chart Example From Scratch and its application with practical example.
In this Laravel Google Bar Chart Example Tutorial I’ll will show how to use google Bar chart in laravel application. In this tutorial you will learn to implement google Bar char in laravel project. With Google Charts we can visualize data in your laravel web application. In this tutorialI’ll share example to demonstrate use of google Bar chart in laravel.
Laravel 7 Google Bar Chart Example From Scratch
- Step 1: Install Laravel App
- Step 2: Add Database Details
- Step 3: Generate Migration & Model File
- Step 4: Add Route
- Step 5: Create Controller
- Step 6: Create Blade File
- Step 7: Run Development Server
Step 1: Install Laravel App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Add Database Configuration
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password |
Step 3: Generate Migration & Model File
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Order -fm |
Then go to app directory and open Order.php file. And put the following code into your Order.php file:
1 2 3 4 5 6 7 8 9 10 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Order extends Model { protected $guarded = []; } |
After that go to database/migrations/ and open create_orders_table.php file and update the following code:
1 2 3 4 5 6 7 |
Schema::create('orders', function (Blueprint $table) { $table->id(); $table->string("product_name")->nullable(); $table->string("product_id")->nullable(); $table->string("price")->nullable(); $table->timestamps(); }); |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
After that, go to database/factories folder and open OrderFactory.php file. And the following code into your OrderFactory.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php /** @var \Illuminate\Database\Eloquent\Factory $factory */ use App\Order; use Faker\Generator as Faker; $factory->define(Order::class, function (Faker $faker) { return [ "product_name" => $faker->word, "product_id" => $faker->numberBetween(1,100), "price" => $faker->numberBetween(1, 100), ]; }); |
Now run the following command to generate fake data into your database in laravel:
1 |
php artisan tinker //then factory(\App\Order::class,100)->create() |
Step 4: Add Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 |
Route::get("google-bar-chart", "OrderController@index"); |
Step 5: Create Controller
Now, lets create a controller named OrderController using command given below –
1 |
php artisan make:controller OrderController |
Then go to app/http/controller folder and open OrderController.php. And put the following code into your OrderController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Order; class OrderController extends Controller { public function index() { $orders = Order::all(); return view('google-bar-chart',['orders' => $orders]); } } |
Step 6: Create Blade File
In this step, we will go to /resources/views/ folder and create a blade view file name google-bar-chart.blade.php. And put the following code into your google-bar-chart.blade.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!doctype html> <html lang="en"> <head> <title>Laravel Google Bar Chart Example Tutorial</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> </head> <body> <h2 style="text-align: center;">Laravel Google Bar Charts Example Tutorial</h2> <div class="container-fluid p-5"> <div id="barchart_material" style="width: 100%; height: 500px;"></div> </div> <script type="text/javascript"> google.charts.load('current', {'packages':['bar']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Order Id', 'Price', 'Product Name'], @php foreach($orders as $order) { echo "['".$order->id."', ".$order->price.", ".$order->Product_name."],"; } @endphp ]); var options = { chart: { title: 'Bar Graph | Price', subtitle: 'Price, and Product Name: @php echo $orders[0]->created_at @endphp', }, bars: 'vertical' }; var chart = new google.charts.Bar(document.getElementById('barchart_material')); chart.draw(data, google.charts.Bar.convertOptions(options)); } </script> </body> </html> |
Step 7: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 2 3 4 5 |
php artisan serve If you want to run the project diffrent port so use this below command php artisan serve --port=8080 |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/google-bar-chart |