DEV Community

Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on

6 1

How to create dynamic rows of table in Laravel

Hello Readers,

In laravel while designing blade file for PDF we require plain CSS only we cannot use any frameworks like bootstrap, tailwind CSS, Bulma, etc., and at same time to show data by grid system we have to use table.

But in some cases we require the rows of the table where the first row required the two columns and the second row requires three columns, this is possible when the data is static but it gets complicated when we have the dynamic.

To do that we use modulo operator (%) through which we can define these type of rows.

Refer the below code for better understanding.

$students = [
  {
    name: John,
    last_name: Kim
  },
  {
    name: Lee Min,
    last_name: Hoo
  },
  {
    name: Park,
    last_name: Shin
  },
  {
    name: Nick,
    last_name: J
  },
  {
    name: Kiara,
    last_name: Advani
  },
 {
    name: Jack,
    last_name: Den
  },
];
<table>

            @foreach ($students as $key => $student)
                @if ($key % 2 == 0)
                    echo '<tr class = "pdf-content">';
                @endif

                echo "<td class='width-50px'>
                    <br>
                    <span>
                        <b>Student's Name</b>
                        {{$student->first_name}} {{$student->last_name}}
                    </span>
                    <br>

                    <span>
                        <b>Username</b>
                       {{ $student->username}}
                    </span>
                    <br>

                    <span>
                        <b>Password</b>
                        {{$student->student_unhashed}}
                    </span>

                   </td> ";

                @if ($key % 2 == 1)
                    echo '</tr>';
                @endif
            @endforeach

        </table>
Enter fullscreen mode Exit fullscreen mode

Thank you for reading 🊄

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs