A
A
Amondullo2020-07-10 12:45:01
Laravel
Amondullo, 2020-07-10 12:45:01

How to save named multiple photos (multi upload) to database in Laravel 5.4?

Greetings!

Learning Laravel 5.4 creating an online store.
At the moment, my first application is an online store.

The product must have a photo gallery.
Photos are uploaded to the specified folder.
Question:
How to store filenames in one column in the database?

Thank you in advance!
The form

<div class="form-group">
    <label for="imageInput">File input</label>
    <input required=""  data-preview="#preview"  name="input_img[]"  type="file" id="imageInput" multiple>
</div>


Controller:
$paths = [];
        foreach($request->input_img as $imag) {

            $image = $imag->getClientOriginalName();

        $actual_name = pathinfo($image,PATHINFO_FILENAME);
        $original_name = $actual_name;
        $extension = pathinfo($image, PATHINFO_EXTENSION);

        $i = 1;
        while(file_exists('images/products/'.$actual_name.".".$extension))
        {
            $actual_name = (string)$original_name.$i;
            $image = $actual_name.".".$extension;
            $i++;
        }

        $imag->move(Config::get('app.upload_path') . 'images/products', $image);
        $dir=public_path('images/products/thumbnails/');
        if( ! \File::isDirectory($dir) ) {
            \File::makeDirectory($dir, 493, true);
        }
    
    
        }
    
        $product->image = $paths;

        $product->save();


Mistake
(1/1) ErrorException
Array to string conversion

in AdminProductController.php line 95
at HandleExceptions->handleError(8, 'Array to string conversion

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pochepko, 2020-07-10
@Amondullo

Make a one-to-many relationship with the Image object in the DB.
You will attach images to the product.
And in the image object itself, store ID, originalName, path, product_id.
In this case, you will not have a mess in your head and you will figure out how it works, and the scheme is more transparent.
In the controller, after saving the image, create an object for it and save it in the database with the necessary connections

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question