satriamuda's avatar

how do show multiple image laravel

Many uploaded images don't appear, they are already in the database, please help

model

   public function store(Request $request)
{
    $galleryPaths = [];

    if ($request->hasFile('gallery')) {
        foreach ($request->file('gallery') as $gallery) {
            $galleryPath = $gallery->store('images/furniture');
            $galleryPaths[] = $galleryPath;
        }
    }

    $property = property::create([
        'gambar' => $request->hasFile('gambar') ? $request->file('gambar')->store('images/furniture') : null,
        'gallery' => implode(',', $galleryPaths),
        'tipe_properti' => $request->tipe_properti,
        'title' => $request->title,
        'deskripsi' => $request->deskripsi,
        'maps' => $request->maps,
        'harga' => $request->harga,
        'lb' => $request->lb,
        'lt' => $request->lt,
        'km' => $request->km,
        'kt' => $request->kt,
        'parkir' => $request->parkir,
        'sertifikat' => $request->sertifikat,
        'dayalistrik' => $request->dayalistrik,
        'interior' => $request->interior,
        'hadap' => $request->hadap,
        'konsep' => $request->konsep,
        'fasilitas' => implode(', ', $request->fasilitas),
        'transportasi' => $request->has('transportasi') ? implode(', ', $request->transportasi) : null,
        'catatan_agent' => $request->catatan_agent ?? null,
        'whatsapp' => $request->whatsapp,
        'slug' => Str::slug($request->title, '-'),
    ]);

    return redirect()->back()->with('success', 'Data properti berhasil disimpan');
}

controller index slug


   public function propertidetail($slug) {



    return view('frond.propertydetail', [

        'properti' => property::where('slug', $slug)->get(),
    ]);

show blade

 <section class="bg-top-74 d-table w-100">
        <div class="container-fluid">

            @foreach ($properti as $item)
                <div class="row">

                    <div class="col-lg-6 p-1">
                        <div
                            class="work-container work-primary work-modern position-relative d-block overflow-hidden rounded">
                            <img src="{{ asset($item->picture) }}" class="img-fluid rounded shadow"
                                style="width: 951px; height: 717px;" alt="">
                            <div class="overlay-work"></div>
                            <div class="icons text-center">
                                <a href="{{ asset($item->picture) }}"
                                    class="work-icon bg-white d-inline-flex rounded-pill lightbox"><i
                                        data-feather="camera" class="fea icon-sm image-icon"></i></a>
                            </div>
                        </div>
                    </div><!--end col-->




                    <div class="col-lg-6">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-12 p-1">
                                        <div
                                            class="work-container work-primary work-modern position-relative d-block overflow-hidden rounded">
                                            @foreach (explode(',', $item->gallery) as $path)
                                                <img src="{{ asset($path) }}" class="img-fluid rounded shadow"
                                                    alt="">
                                            @endforeach

                                            <div class="overlay-work"></div>
                                            <div class="icons text-center">
                                                <a href="assets/images/real/property/single/2.jpg')}}"
                                                    class="work-icon bg-white d-inline-flex rounded-pill lightbox"><i
                                                        data-feather="camera" class="fea icon-sm image-icon"></i></a>
                                            </div>
                                        </div>
                                    </div><!--end col-->

                                    <div class="col-12 p-1">
                                        <div
                                            class="work-container work-primary work-modern position-relative d-block overflow-hidden rounded">
                                            <img src="{{ asset('pro/assets/images/real/property/single/3.jpg') }}"
                                                class="img-fluid rounded shadow" alt="">
                                            <div class="overlay-work"></div>
                                            <div class="icons text-center">
                                                <a href="{{ asset('pro/assets/images/real/property/single/3.jpg') }}"
                                                    class="work-icon bg-white d-inline-flex rounded-pill lightbox"><i
                                                        data-feather="camera" class="fea icon-sm image-icon"></i></a>
                                            </div>
                                        </div>
                                    </div><!--end col-->
                                </div><!--end row-->
                            </div><!--end col-->

                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-12 p-1">
                                        <div
                                            class="work-container work-primary work-modern position-relative d-block overflow-hidden rounded">
                                            <img src="{{ asset('pro/assets/images/real/property/single/4.jpg') }}"
                                                class="img-fluid rounded shadow" alt="">
                                            <div class="overlay-work"></div>
                                            <div class="icons text-center">
                                                <a href="{{ asset('pro/assets/images/real/property/single/4.jpg') }}"
                                                    class="work-icon bg-white d-inline-flex rounded-pill lightbox"><i
                                                        data-feather="camera" class="fea icon-sm image-icon"></i></a>
                                            </div>
                                        </div>
                                    </div><!--end col-->

                                    <div class="col-12 p-1">
                                        <div
                                            class="work-container work-primary work-modern position-relative d-block overflow-hidden rounded">
                                            <img src="{{ asset('pro/assets/images/real/property/single/5.jpg') }}"
                                                class="img-fluid rounded shadow" alt="">
                                            <div class="overlay-work"></div>
                                            <div class="icons text-center">
                                                <a href="{{ asset('pro/assets/images/real/property/single/5.jpg') }}"
                                                    class="work-icon bg-white d-inline-flex rounded-pill lightbox"><i
                                                        data-feather="camera" class="fea icon-sm image-icon"></i></a>
                                            </div>
                                        </div>
                                    </div><!--end col-->
                                </div><!--end row-->
                            </div><!--end col-->
                        </div><!--end row-->
                    </div><!--end col-->


                </div><!--end row-->
            @endforeach
        </div> <!--end container-->
    </section><!--end section-->
0 likes
3 replies
Tray2's avatar

There are so many issues with you code and design.

  1. You must validate the request.
  2. Store the image paths in it's own table not as a comma separated list in one the properties table.
  3. Class names should always start with a capital letter. Property not property.
  4. Where does $item->picture come from, there is no such column in the properties table.
satriamuda's avatar

@Tray2 this public function getPictureAttribute() { return asset ('storage/'. $this->gambar); }

LaryAI's avatar
Level 58

It seems like the main issue you're encountering is related to displaying multiple images stored in a single database field as a comma-separated string. Here's how you can modify your code to handle and display these images correctly in your Blade template.

Step 1: Modify the Controller

Ensure that your controller is passing the correct data to the view. It looks like you're already fetching the property based on the slug, which is good. However, using get() will return a collection. If you are expecting a single property, you should use first() instead of get().

public function propertidetail($slug) {
    $property = property::where('slug', $slug)->first();
    return view('frond.propertydetail', [
        'properti' => $property,
    ]);
}

Step 2: Update the Blade Template

In your Blade template, you need to handle the display of multiple images. Since you're storing paths in a comma-separated string, you can use explode() to split the string into an array of image paths. Here's how you can update your Blade template to loop through these paths and display each image:

<section class="bg-top-74 d-table w-100">
    <div class="container-fluid">
        @if($properti)
            <div class="row">
                <div class="col-lg-6 p-1">
                    <div class="work-container work-primary work-modern position-relative d-block overflow-hidden rounded">
                        <img src="{{ asset($properti->gambar) }}" class="img-fluid rounded shadow" style="width: 951px; height: 717px;" alt="">
                        <div class="overlay-work"></div>
                        <div class="icons text-center">
                            <a href="{{ asset($properti->gambar) }}" class="work-icon bg-white d-inline-flex rounded-pill lightbox"><i data-feather="camera" class="fea icon-sm image-icon"></i></a>
                        </div>
                    </div>
                </div><!--end col-->

                <div class="col-lg-6">
                    <div class="row">
                        @foreach (explode(',', $properti->gallery) as $path)
                            <div class="col-md-6 p-1">
                                <div class="work-container work-primary work-modern position-relative d-block overflow-hidden rounded">
                                    <img src="{{ asset($path) }}" class="img-fluid rounded shadow" alt="">
                                    <div class="overlay-work"></div>
                                    <div class="icons text-center">
                                        <a href="{{ asset($path) }}" class="work-icon bg-white d-inline-flex rounded-pill lightbox"><i data-feather="camera" class="fea icon-sm image-icon"></i></a>
                                    </div>
                                </div>
                            </div><!--end col-->
                        @endforeach
                    </div><!--end row-->
                </div><!--end col-->
            </div><!--end row-->
        @endif
    </div> <!--end container-->
</section><!--end section-->

Explanation:

  1. Single Image Display: The main image (gambar) is displayed separately.
  2. Multiple Images Display: The gallery images are split into an array using explode(',', $properti->gallery), and each image is displayed in a loop.

This should correctly display both the main image and all gallery images for your property. Make sure that the paths stored in the database are correct and accessible from your public directory.

Please or to participate in this conversation.