为了账号安全,请及时绑定邮箱和手机立即绑定

在 Laravel 中,我很难理解使用 @foreach 遍历刀片中的数组所需的语法

在 Laravel 中,我很难理解使用 @foreach 遍历刀片中的数组所需的语法

PHP
qq_花开花谢_0 2022-12-11 09:16:36
我在读取从控制器传递到刀片的以下“$photos”数组时遇到问题。  0 => array:2 [▼    "destinationPath" => "images/"    "filename" => "15-1-tue-apr-7-2020-130-am-34824.jpg"  ]  1 => array:1 [▼    0 => array:2 [▼      "destinationPath" => "images/"      "filename" => "15-1-tue-apr-7-2020-130-am-89914.jpg"    ]  ]  2 => array:1 [▼    0 => array:2 [▼      "destinationPath" => "images/"      "filename" => "15-1-tue-apr-7-2020-130-am-30958.jpg"    ]  ]  3 => array:1 [▼    0 => array:2 [▼      "destinationPath" => "images/"      "filename" => "15-1-tue-apr-7-2020-130-am-68870.jpg"    ]  ]]如果我直接在刀片中调用照片,如下所示,它可以拉出图像:  <img class="img-fluid options-item" src="{{$path}}/{{ $photos[0]['destinationPath'] }}{{ $photos[0]['filename'] }}" alt="">但是当我尝试遍历数组时@for ($i = 0; $i < count($photos); $i++)  <img class="img-fluid options-item" src="{{$path}}/{{ $photos[$i]['destinationPath'] }}{{ $photos[$i]['filename'] }}" alt="">@endfor我收到以下错误:Facade\Ignition\Exceptions\ViewExceptionUndefined index: destinationPath (View: C:\Apache24\htdocs\collection\proof\resources\views\pages\gallery.blade.php)我还尝试了以下结果是否定的:@foreach ($photos as $photo)   <img class="img-fluid options-item" src="{{$path}}/{{ $photo['destinationPath'] }}{{ $photo['filename'] }}" alt="">@endforeach结果:Facade\Ignition\Exceptions\ViewExceptionUndefined index: destinationPath (View: C:\Apache24\htdocs\collection\proof\resources\views\pages\gallery.blade.php)任何关于正确语法的指导将不胜感激。
查看完整描述

1 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

在这里,您首先使用的是@for,但最终使用的是@endforeach。你想用哪一个?for循环还是foreach循环?


@for ($i = 0; $i < count($photos); $i++)

  <img class="img-fluid options-item" src="{{$path}}/{{ $photos[$i]['destinationPath'] }}{{ $photos[$i]['filename'] }}" alt="">

@endforeach

如果你想使用foreach


@foreach ($photos as $photo)

 <img class="img-fluid options-item" src="{{$path}}/{{ $photo->destinationPath }}{{ $photo->filename }}" alt="">

@endforeach

您收到错误的原因是$photo['destinationPath']数组语法。$photo实际上是object。所以你需要使用->来访问它的属性。


查看完整回答
反对 回复 2022-12-11
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信