0

I have the following array result set, I'm trying to loop through each of the results and just echo them out onto the page. I'm using Laravel 5.2 and the blade templating engine

Collection {#240 ▼
  #items: array:3 [▼
    0 => array:2 [▼
      "name" => "desktop"
      "views" => "349"
    ]
    1 => array:2 [▼
      "name" => "mobile"
      "views" => "151"
    ]
    2 => array:2 [▼
      "name" => "tablet"
      "views" => "68"
    ]
  ]
}

This is what I have so far

@foreach($devices as $device)
    $key = 0; $key++; $key < 2;
    {{ $device[$key] }},
@endforeach

3 Answers 3

2
@foreach($devices as $device)
    {{ $device->name }}

    {{ $device->views}}
@endforeach

Will be enough.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to echo object properties:

@foreach($devices as $device)
    {{ $device->name }} has {{ $device->views }}
@endforeach

Comments

1

If you like to use key then

@foreach($devices as $key => $val)
     {{ $device[$key]->name }},
     {{ $device[$key]->views }}
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.