在Laravel视图中使用foreach on array

时间:2015-07-30 22:36:29

标签: php arrays laravel

为什么我的foreach循环失败并显示错误“试图获取非对象的属性”:

@foreach ($memberships as $membership)
    {{ $membership->id }}
@endforeach 

但这很好用:

@foreach ($memberships as $membership)
    <?php print_r($membership['id']); ?>
@endforeach 

如果我dd($memberships);

array:2 [▼
  0 => array:1 [▼
    "id" => 8
  ]
  1 => array:1 [▼
    "id" => 9
  ]
]

1 个答案:

答案 0 :(得分:2)

正如所指出的那样,我试图将数组作为对象来访问......

@foreach ($memberships as $membership)
    {{ $membership['id'] }}
@endforeach