laravel Bar charts from database Example chart |
laravel Bar charts from database Example chart |

laravel Bar charts from database Example | chart.js


In web development, data visualization is an essential aspect to convey information effectively. laravel Bar charts are one of the most common ways to represent data visually. Laravel, being a popular PHP framework, offers various methods to integrate charts into your web applications. One convenient way is to utilize Content Delivery Networks (CDN) for chart libraries, simplifying the process of adding interactive charts to your Laravel projects. In this tutorial, we’ll walk through the process of showing data in bar charts using CDN in Laravel.


Before diving into this tutorial, make sure you have the following prerequisites:

  1. Basic understanding of Laravel framework.
  2. Composer installed on your system.
  3. Basic knowledge of HTML, CSS, and JavaScript.

Setting Up Laravel: If you haven’t already set up a Laravel project, you can do so by running the following command in your terminal:

composer create-project --prefer-dist laravel/laravel my-chart-project

Replace “my-chart-project” with your desired project name.

Once Laravel is installed, navigate to your project directory:

cd my-chart-project

Creating a Route:

Open the routes/web.php file in your Laravel project and define a route for displaying the chart:

use App\Http\Controllers\ChartController;

Route::get('/chart', [ChartController::class, 'showChart']);

Creating a Controller:

Next, let’s create a controller named ChartController using the following command:

php artisan make:controller ChartController

This command will generate a new controller file under the app/Http/Controllers directory.

Open the ChartController.php file and define a method named showChart:


namespace App\Http\Controllers;

class ChartController extends Controller
    public function showChart()
        $data = [20, 30, 40, 50, 60]; // Example data
        return view('chart', compact('data'));

Creating a View:

Now, let’s create a view file named chart.blade.php where we’ll render the chart:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bar Chart Example</title>
    <!-- Include Chart.js CDN -->
    <script src=""></script>
    <canvas id="myChart" width="400" height="400"></canvas>
        var ctx = document.getElementById('myChart').getContext('2d');
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ['Data 1', 'Data 2', 'Data 3', 'Data 4', 'Data 5'],
                datasets: [{
                    label: 'Data',
                    data: <?php echo json_encode($data); ?>,
                    backgroundColor: 'rgba(54, 162, 235, 0.2)',
                    borderColor: 'rgba(54, 162, 235, 1)',
                    borderWidth: 1
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true

In this view, we include the Chart.js library using a CDN link. We then create a canvas element with an id of myChart where the chart will be rendered. Inside the <script> tag, we use JavaScript to initialize and configure the chart using the provided data.

Testing: Now, you can test your Laravel application by running the following command in your terminal:

php artisan serve

Open your web browser and navigate to http://localhost:8000/chart. You should see a bar chart rendered with the sample data provided in the controller.


In this tutorial, we’ve demonstrated how to show data in bar charts using a CDN in Laravel. By leveraging the Chart.js library and Laravel’s routing and controller capabilities, you can easily integrate dynamic and interactive charts into your web applications. Experiment with different chart types and customization options to enhance the visualization of your data. Happy coding!

Leave a Reply