'How to get array values using foreach in laravel

I am new to laravel and php. I have this code in one of my controllers:

$group_by_precedence = array("true", "false");

And this code in the view:

@foreach ($group_by_precedence as $value)
    <thead>
        <tr>
            <th colspan="8">
                <a class="collapse-button collapsed"
                    data-toggle="collapse"
                    href="#collapse_&&&&&"
                    aria-expanded="true"
                    aria-controls="collapse_&&&&&"
                    onclick="loadDealsFor(&&&&&, 'precedence');"
                    style="width: 100%; display: block; margin: -10px; padding: 10px;"
                >
                    <i class="zmdi zmdi-plus"></i> <i class="zmdi zmdi-minus"></i> exists
                </a>
            </th>
        </tr>
    </thead>
@endforeach

I tried but I did not manage to duplicate the code twice, and replace the &&&&& with the values from the $group_by_precedence array; first true and then false.

Currently the bad solution is to duplicate the code in the view, and just change the &&&&& with true and false.



Solution 1:[1]

Hope it might help:

<thead>
    <tr>
        @foreach ($group_by_precedence as $value)
            <th colspan="8">
                <a class="collapse-button collapsed"
                    data-toggle="collapse"
                    href="#collapse_{{ $value }}"
                    aria-expanded="true"
                    aria-controls="collapse_{{ $value }}"
                    onclick="loadDealsFor({{ $value }}, 'precedence');"
                    style="width: 100%; display: block; margin: -10px; padding: 10px;"
                >
                    <i class="zmdi zmdi-plus"></i> <i class="zmdi zmdi-minus"></i> exists
                </a>
            </th>
        @endforeach
    </tr>
</thead>

Solution 2:[2]

In your blade file you can print the vars with {{ $var }} . like that:

$group_by_precedence = array("true", "false");

@foreach ($group_by_precedence as $value)
    <p>Your value is: {{ $value }}</p>
@endforeach

Solution 3:[3]

Tnx it is solved

@foreach ($group_by_precedence as $value)                   
                   
                        <thead>
                            <tr>
                              
                                <th colspan="8">
                                    <a class="collapse-button collapsed" data-toggle="collapse" href="#collapse_{{ $value }}" aria-expanded="true" aria-controls="collapse_{{ $value }}" onclick = "loadDealsFor('{{ $value }}', 'precedence');"  style="width: 100%;display: block;margin: -10px;padding: 10px;">
                                        <i class="zmdi zmdi-plus"></i> <i class="zmdi zmdi-minus"></i>exists
                                    </a>
                                </th>
                                   
                            </tr>
                        </thead>
                        <tbody id="collapse_{{ $value }}" class="collapse" aria-expanded="false" data-loading="no">
                            <tr>
                                <td colspan="8" id="collapse_{{ $value }}_loader" class="force-white"></td>
                            </tr>   
                        </tbody>
                    @endforeach

code is ok but dont u think this will create wrong id because u placed the whole object $value rather than accesing through id like {{ $value->id}}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 matiaslauriti
Solution 2 matiaslauriti
Solution 3 VANITA CHAUDHARI