简体   繁体   English

在Laravel中显示数组中的多个图像

[英]Display multiple images from an array in Laravel

I am a Laravel newbie and this is my problem. 我是Laravel新手,这是我的问题。 I have this piece of code below that works inside my controller. 我下面有这段代码可以在我的控制器中使用。

public function home()
    return view('/home', [
    'image' => 'img/ctewzvv-aweadyojaae1cwbrvlo192.png',
    ]);
}

Inside my partial is this: 在我的部分里面是这样的:

<div class="img-container">
     <a href="#">
          <img src="{{ asset( $image ) }}" alt="image">
     </a>
</div>

They work perfectly fine. 他们工作得很好。 My question is what if I have multiple image paths that I want to display inside a list like this: 我的问题是,如果我想在列表中显示多个图像路径,该怎么办:

public function home() {
     $images = ['img/ctewzvv-aweadyojaae1cwbrvlo192.png','img/abcwzvv-aweadyojaae1cwbrvlo192.png','img/vbfwzvv-aweadyojaae1cwbrvlo192.png'];
     return view('/home', [
         'images' => $images
     ]);
}

Would the above code possible? 上面的代码可能吗?

Or what if I have all the images contained inside a folder, would it be possible if I display them at once? 或者,如果我将所有图像都包含在一个文件夹中,该如何显示一次? Thanks in advance. 提前致谢。 Help is much appreciated. 非常感谢您的帮助。 Please respect. 请尊重。

you should try this: 您应该尝试这样:

public function home() {
     $images = ['img/ctewzvv-aweadyojaae1cwbrvlo192.png','img/abcwzvv-aweadyojaae1cwbrvlo192.png','img/vbfwzvv-aweadyojaae1cwbrvlo192.png'];
     return view('/home', [
         'images' => $images
     ]);
}

View page: 查看页面:

@foreach($images as $image)
<div class="img-container">
     <a href="#">
          <img src="{{ asset( $image ) }}" alt="image">
     </a>
</div>
@endforeach

Yes you can add a loop in your blade file and show all the images: 是的,您可以在刀片文件中添加一个循环并显示所有图像:

@foreach($images as $image)
    <img src="{{ asset( $image ) }}" alt="image">
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM