kazzuya's avatar

Multiple photo

Hello, Can i know what the wrong with that code ?

        foreach ($this->images as $image) {
        $extension = $image->getClientOriginalExtension();
        $filename = time() . '.' . $extension;
        $image->storeAs('product_multiple_image', $filename, 'real_public');
        $this->images = $filename;
        ProductImage::create([ 'product_id' => $this->product->id, 'image' => $filename ]);
    }

It's take the multiple image in database but all photos as 1 , That mean if i uploaded 3 photos in database they are 3 but in images folder only 1 photo

My blade

<input type="file" id="image" wire:model="images" name="image[]"
                                class="form-control" multiple />
0 likes
10 replies
thinkverse's avatar

It could be because you're overwriting $this->images with the $filename during the first run of the loop before you store it in the database.

$this->images = $filename;
ProductImage::create([ 'product_id' => $this->product->id, 'image' => $filename ]);
kazzuya's avatar

@thinkverse I tried it like that and it stored in database

        foreach ($this->images as $image) {
        $extension = $image->getClientOriginalExtension();
        $filename = time() . '.' . $extension;
        $filename = $image->storeAs('product_multiple_image', $filename, 'real_public');
        ProductImage::create([ 'product_id' => $this->product->id, 'image' => $filename ]);
    }

But didn't store in the 'product_multiple_image' folder

kazzuya's avatar

@MichalOravec Sorry, In the folder it's done the photos in uploaded , But not showed for user, That's my blade

                    @foreach ($productimage as $images)
                        <div class="col-4">
                            <img src="{{ asset('/product_multiple_image') . '/' . $images->image }}" />
                        </div>
                    @endforeach
MohamedTammam's avatar

Here's how I do it.

        foreach ($this->images as $image) {
        $filePath = $image->store('product_multiple_image', 'public'); // Store the file with a unique name in the "public" driver
        ProductImage::create([ 'product_id' => $this->product->id, 'image' => $filePath ]);
    }
 @foreach ($productimage as $images)
<div class="col-4">
	<img src="{{ Storage::url($images->image) }}" />
</div>
@endforeach

Please or to participate in this conversation.