1

I am a Laravel newbie and this is my problem. 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.

2
  • Yes it is possible if you wrap your partial code with an @foreach($images as $image) Commented Dec 14, 2018 at 6:42
  • Hi @DestinatioN. That's interesting, I will try that out. How about if I just call all the images inside a folder like "*.jpg". Will this work? Commented Dec 14, 2018 at 6:44

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

I'll definitely try this. Thanks @Saurabh
It works my friend. Then I put the @foreach inside the div. Many thanks.
@DaveMoreno: Glad to help :) and please accept my answer
I tried accepting the answer but I guess I can only pick one. I also tried hitting the button up for usefulness but it says below 15 reputation is just recorded. Anyways, thank you for the answer.
@DaveMoreno: Okay
1

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

1 Comment

I will try this my friend. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.