Laravel + Blade: How to Render Array Data with Tables

Updated: February 6, 2024 By: Guest Contributor Post a comment

Introduction

In this comprehensive guide, we will explore how to efficiently render array data within tables using Laravel, a prominent PHP framework, and its templating engine, Blade. Laravel facilitates web application development with its elegant syntax and features, whereas Blade enhances these capabilities by allowing for the seamless embedment of PHP code in HTML. We will embark on a step-by-step journey, delving into the creation of a dynamic table that renders array data efficiently. This tutorial is designed for both beginners who are just getting started with Laravel and seasoned developers looking to refine their skills.

Prerequisites

  • Basic understanding of PHP and HTML.
  • Laravel installed on your development machine (Laravel 8.x is used in examples).
  • A text editor (VS Code, Sublime, etc.) and a web browser.

Step 1: Setting Up Your Laravel Project

Firstly, ensure that Laravel is installed. If you need to install Laravel, run the following command in your terminal:

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

Next, navigate into your project directory:

cd ArrayTableProject

Now, with Laravel installed, you’re ready to start developing your application.

Step 2: Preparing the Controller

We will begin by creating a controller that will handle the data for our table. Use the Artisan command line tool provided with Laravel to create a new controller:

php artisan make:controller DataController

In your newly created DataController.php, add a method to return some sample array data. For the sake of this tutorial, we’ll return a statically defined array:

public function show()
{
    $data = [
        ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
        ['id' => 2, 'name' => 'Jane Doe', 'email' => '[email protected]'],
        // Add more sample data as needed
    ];

    return view('show-data', compact('data'));
}

Add a route for this controller method in your web.php file:

Route::get('/data', '[DataController::class, 'show']);

Step 3: Creating a Blade View

Next, create a new Blade file named show-data.blade.php in the resources/views directory. In this file, we’ll construct a table to display our array data.

<!DOCTYPE html>
<html>
<head>
    <title>Show Data</title>
</head>
<body>
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
        @foreach ($data as $item)
        <tr>
            <td>{{ $item['id'] }}</td>
            <td>{{ $item['name'] }}</td>
            <td>{{ $item['email'] }}</td>
        </tr>
        @endforeach
    </table>
</body>
</html>

This Blade template iterates over the $data array passed from our controller and dynamically fills the table rows with array values. The @foreach directive is a Blade construct that provides a more concise way to write PHP loops inside HTML.

Step 4: Styling the Table

Although the focus of this tutorial is on functionality rather than appearance, adding minimal CSS can enhance the readability of your table. In the <head> section of your Blade file, add the following style:

<style>
table {
    border-collapse: collapse;
    width: 100%;
}
th, td {
    padding: 8px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}
</style>

Step 5: Running Your Application

To see your dynamic table in action, start the Laravel development server:

php artisan serve

Navigate to http://127.0.0.1:8000/data in your web browser. You should now see your table filled with data sourced from the array in your controller.

Conclusion

Through this tutorial, we’ve explored how to use Laravel and Blade to dynamically render array data within an HTML table. By following the steps outlined, you’ve learned how to set up a Laravel project, create a controller, return array data, and display this data in a Blade view using table structure. Utilizing Laravel’s elegant syntax alongside Blade’s templating capabilities allows for the efficient development and rendering of dynamic web content. This skill set is indispensable for developing data-driven web applications with Laravel.