maaz's avatar
Level 2

How to show array of images in my blade view?

I want to show array of images in my blade view. I have a table which has some fields like name, address, images. where images are more than one how I can show multiple images in my blade-view.

blade view

 <div class="business-gallery">
                        <div class="gallery">
                            {{-- {{dd($business->image)}} --}}
                            {{-- @json($business->image) --}}
                            <img src="{{$business['image'][0]}}" alt="Second image" />
                        </div>
                    </div>

for each loop etc doesn't work for me like this,I can even fetch/show single image on my view

@foreach($business['image'] as $image)
  <image src="{{ $business['Name']}}" />
@endforeach

please help me out

0 likes
17 replies
maaz's avatar
Level 2

@jlrdw i dont want to style them etc i did it already. i want to show those images on front end. 😶

jlrdw's avatar

Well I am old school I would do 3 divs floated left, then a clear float and repeat for the number of rows you need.

Or a table.

But if you watch some of those videos that's not just for style he positiions stuff all over the place with a grid system.

Remember CSS has to do with positioning also.

But watching that series was only a suggestion.

Snapey's avatar

If the images are in an array, just use an @foreach loop ?

You need to understand your data first though. You have to know if the database contains just the image name or the full path or url in order to construct a valid src attribute.

For example, outputting slides ;

@foreach($hiw->slides as $slide)
    <li class="glide__slide">
        <img class="w-full rounded-lg" src="{{ $slide->slideURL() }}" />
    </li>
@endforeach

I can only do it this simply because I created a slideURL function in my model which returns the correct url for each image.

RobsonZindoga1's avatar

you may embrace laravel by having a loop like this @foreach($slide as $slide) <img src="{{asset("storage/$slide->image")}}" alt="No image to show"class="img-responsive img- fluid" > @endforeach

maaz's avatar
Level 2

@snapey i tried it like this which is not working for me. images are there when i did {{dd($business->image)}} or blade directive @json($business->image) so it give me result. but when i want to print/show images inside image tag it didnt. ask me for some code or anything so you can check where am wrong. thankyou

MichalOravec's avatar

@maaz What is structure of your $business->image?

If you have there array of path then

@foreach($business->image as $image)
  <img src="{{ $image }}">
@endforeach
maaz's avatar
Level 2

@michaloravec

   public function store(Request $request)
    {
        if ($request->hasFile('pictures')) {
            foreach ($request->file('pictures') as $picture) {
                $pictures[] = $fileName = time() . $picture->getclientOriginalName();
                Storage::put('public/' . $fileName, file_get_contents($picture));
            }
            Business::create([
                'image' => implode(',', $pictures),
                'name' => $request->name,
                'price' => $request->input('price'),
                'description' => $request->input('description'),
                'user_id' => $request->input('user'),
                'category_id' => $request->input('category'),
                'subCategory_id' => $request->input('subCategory'),
                'province_id' => $request->input('province'),
                'city_id' => $request->input('city'),
            ]);
        }
        return redirect('sell-business-form')->with('success', 'Your business is under review, Please wait for confirmation');
    }

this is my code to store images + other data into db

maaz's avatar
Level 2

@michaloravec and from here I am passing data to view. check it out

 public function businessDetail($id)
    {
        $business = Business::findOrFail($id);
        return view('front-end.business-detail', compact('business'));
    }
maaz's avatar
Level 2

@michaloravec let me know. what if i want to show only one image from that array . which will be the featured image for a single business.

maaz's avatar
Level 2

Oh Dear. Thanks aloot i was stuck in this method from the past two day . Thanks aloot :)

Please or to participate in this conversation.