【问题标题】:Laravel array result set - Use of undefined constant nameLaravel 数组结果集 - 使用未定义的常量名
【发布时间】:2017-06-18 11:49:11
【问题描述】:

我正在使用 Laravel 5.2 并使用以下方法将数组结果集返回到我的视图中

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

我尝试在刀片中使用以下内容循环遍历我的数组数据

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

使用$device[name] 抛出Use of undefined constant name - assumed 'name'

我也试过像这样循环遍历结果

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

【问题讨论】:

标签: php arrays laravel laravel-5


【解决方案1】:

您将其发送为常量而不是字符串。像这样替换它:

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

【讨论】:

  • 即使我将两个键都用作字符串“name”和“views”,我也得到了两次相同的结果
  • 尝试像这样打印设备数组:{!! dd($devices) !!} 并在此处打印结果。
猜你喜欢
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 2012-01-27
  • 2017-10-24
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
相关资源
最近更新 更多