How to make header table with foreach in laravel?

by wilmer.lemke , in category: Third Party Scripts , 4 months ago

How to make header table with foreach in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 4 months ago

@wilmer.lemke 

To create a header table using a foreach loop in Laravel, you can follow these steps:

  1. Define the header array in your controller:
1
$headers = ['ID', 'Name', 'Email', 'Phone'];


  1. Pass the header array to your view:
1
return view('your-view', ['headers' => $headers]);


  1. In your view file, loop through the header array and display each header as a table header:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<table>
    <thead>
        <tr>
            @foreach($headers as $header)
                <th>{{ $header }}</th>
            @endforeach
        </tr>
    </thead>
    <tbody>
        <!-- Add table rows here -->
    </tbody>
</table>


  1. You can add table rows with data below the header section by fetching data from your database or any other source:
1
2
3
4
5
6
7
8
@foreach($data as $row)
    <tr>
        <td>{{ $row->id }}</td>
        <td>{{ $row->name }}</td>
        <td>{{ $row->email }}</td>
        <td>{{ $row->phone }}</td>
    </tr>
@endforeach


By following these steps, you can create a header table with foreach loop in Laravel.