0

I'm using Laravel 5.2 and returning an array result set to my view by using the following

return view('home')->with('devices', $devices)

I've attempted to loop through my array data by using the following in blade

@foreach($devices as $device)
    {{ $device[name] }} has
    {{ $device[views] }}
@endforeach

Using $device[name] throws Use of undefined constant name - assumed 'name'

I've also tried looping through the result like this

@foreach($devices as $device)
    {{ $device->name }} has
    {{ $device->views }}
@endforeach
3
  • 1
    lol dude, you're using my code but you've chosen another guy's answer. Commented Feb 1, 2017 at 16:28
  • 1
    Array keys should be quoted. There are exceptions, but this isn't one of them. Commented Feb 1, 2017 at 16:28
  • @AlexeyMezenin In that example we both used the word has? Although the mark up is different.... Commented Feb 1, 2017 at 16:48

1 Answer 1

1

You are sending it like constant not as string. Replace it like this:

@foreach($devices as $device)
    {{ $device['name'] }}
    {{ $device['views'] }}
@endforeach
Sign up to request clarification or add additional context in comments.

2 Comments

I get the same result twice even though I use both keys as a string "name" & "views"
Try printing devices array like this: {!! dd($devices) !!} and print result here.

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.