亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號