Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deepu07's avatar
Level 11

pass dynamic value (key) to the html table

How to pass dynamic value (key) to the html table id ? here is my code

<tbody>
        @foreach($data as $key => $book)
         <tr class="clickable" data-toggle="collapse" id="{{$key}}" data-target=".{{$key}}">

Is that make sense ?? It is not working as expected. Any help that would be great. Thanks!

0 likes
5 replies
jlrdw's avatar

No, not if the key isn't the actual id of the book.

$book->id;

seems that would be an id.

deepu07's avatar
Level 11

@jlrdw like I have one parent table each row will have one child table. based on parent table id I just wanna display the child table here is my sample code

 @foreach($data as $student)
            <tr class="clickable" data-toggle="collapse" id="row1" data-target=".row1">
                <td class="glyphicon glyphicon-plus">{{ $student['id'] }}</td>
            </tr>
            @if ($student['class'])
                <table class="table table-sm table-dark collapse row1">
                    <thead>
                        <tr>
                            <th scope="col">Name</th>
                        </tr>
                    </thead>
                    <tbody>

here instead of row1 i wanna pass a dymanic value. how can I achieve that here? Thanks!

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Do you mean like this?

 @foreach($data as $student)
            <tr class="clickable" data-toggle="collapse" id="row{{$student->id}}" data-target=".row{{$student->id}}">
Sinnbeck's avatar

Also.. A new table should be inside a td

<tr><td>table goes here</td></tr>

Please or to participate in this conversation.