0

I've form in my website. Out of many fields, there is one field that let users allow to upload multiple images. I want to save the path for all images into database so that I can show them back on blade file. I can upload images and images are stored into my local storage but the issue I get only one path and that is saved into database.

I want to store all paths separately into database so that I could access them for Blade. How can I do that?

Blade

<form method="POST" enctype="multipart/form-data" action="{{route('form.multiStepStore')}}">
   @csrf
   <input type="file" class="form-control" name="photos[]" multiple />
......
</form>

Controller

public function multiStepStore(Request $request)
{
    if($request->hasFile('photos')) {
        $allowedfileExtension = ['jpg', 'png', 'jpeg'];
        $files = $request->file('photos');
        foreach ($files as $file) {
            $filename = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $filepath = $filename.'.'.$extension;
            Storage::disk('local')->put($filename.'.'.$extension,  File::get($file));
        }
    $store_seller = new Sellers();
    $store_seller->img_path = $filepath;

    dd($filepath); //Returns only one path out of let's say 3 images
    }
}

Laravel 7. PHP 7.4.

2 Answers 2

1

You need to make an array :

public function multiStepStore(Request $request)
{
    $filepath = array(); // $filepath is now an array
    if($request->hasFile('photos')) {
        $allowedfileExtension = ['jpg', 'png', 'jpeg'];
        $files = $request->file('photos');
        foreach ($files as $file) {
            $filename = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $path = $filename.'.'.$extension;
            Storage::disk('local')->put($filename.'.'.$extension,  File::get($file));
            $filepath[] = $path; // add new image to array
        }
    $store_seller = new Sellers();
    $store_seller->img_path = $filepath;

    dd($filepath); //Returns array with 3 images
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

It's not properly working. I again uploaded 3 files. It only returns 2 paths with the same name. One of the uploaded file was gmail-logo.png. After dd($filepath); it returns the same name but for 2 times.
@Shaan check now, it will work. The mistake was there was a variable name filepath, it makes this array to a variable. So I renamed it to path
I' m seeing all uploaded images into array now that's good. Last step, what If I want to store them into database? I tried implode function but it's giving me error.
@Shaan if you want to store the image name only, then do it under foreach
One question, Is there any purpose of getting file extension. I mean it's look weird when it saves the file name into my storage like "logo.png.png" ?
|
0

Try like this -

public function multiStepStore(Request $request)
    {
        $images[]= Null; // $images array
        if($request->hasFile('photos')) {
            $allowedfileExtension = ['jpg', 'png', 'jpeg'];
            $files = $request->file('photos');
            foreach ($files as $file) {
                $filename = $file->getClientOriginalName();
                $extension = $file->getClientOriginalExtension();
                $filepath = $filename.'.'.$extension;
                Storage::disk('local')->put($filename.'.'.$extension,  File::get($file));
                array_push($images, $filepath);  
            }
            $store_seller = new Sellers();
            $store_seller->img_path = $filepath;
    
        dd($images); //It will returns array with 3 images.
        }
    }

6 Comments

Yes, we are very close. There are only 2 issues. Why I' m getting [0] index. Also, when I tried to implode the array. It returns error. How would I implode the array and store values into variable as string ?
@Shaan What type of error message you are getting on the implode function run?
@Shaan Try with this: $string = implode(',', $images[0]);
The only issue with your suggested code is that It returns 0 index and on upload 3 images, we see the array of 4 values not 3. Other that that your code is working :) .. See the above answer of #STA
If somewhere my code helped you then, Please upvote my answer.
|

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.