Laravel accessing array of sql data for displaying

Viewed 26

I have an array being returned by a DB::Select statement which I would like to pass into a for statement in my page to display, but when doing so am met with the following error: enter image description here

My array looks like such:

Array ( [0] => stdClass Object ( [company_name] => Apple [count(*)] => 2 ) [1] => stdClass Object ( [company_name] => Facebook[count(*)] => 1 ) [2] => stdClass Object ( [company_name] => Netflix [count(*)] => 1 ) ) 

PHP as so:

<div class=" mt-5 text-center">
        @if ($metric_data)
            @foreach ($metric_data as $metric_datas)
                <tr>
                    <td>{{ $metric_data->company_name }}</td>
                    <td>s</td>
                </tr>
            @endforeach
        @else
            <p>No items found.</p>
        @endif
    </div>

I am not sure why It cannot access it? Any help appreciated!

1 Answers

Please change your code with this foreach variables placed wrong that's all

<div class=" mt-5 text-center">
    @if ($metric_data)
        @foreach ($metric_datas as $metric_data)
            <tr>
                <td>{{ $metric_data->company_name }}</td>
                <td>s</td>
            </tr>
        @endforeach
    @else
        <p>No items found.</p>
    @endif
</div>
Related