nurularifin's avatar

How to Add Multiple Data Into Database With One Click Base On How Many Image I Add at Once

Hi everyone, I have made multiple upload images and it works as many tutorials I have followed, but I have one problem, I want to add data to the database base on how many images I select, let's say I select 20 images at once and all that image move to the folder, and also i want to add data to database base on how many image I select, if I select 20 image I want 20 data insert do the database. The following is my controller:

public function store(Request $request)
    {
        $request->validate([
            'images' => 'required',
            'images.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:10240'
        ]);

        $image = array();
        $title = array();

        if (empty($request->images))
        {
            $name = "";
        }
        else
        {
            foreach ($request->file('images') as $imagefile) {
               $image_name  = md5(rand(1000, 10000));
               $ext = strtolower($imagefile->getClientOriginalExtension());
               $image_full_name = $image_name.'.'.$ext;
               $imagefile->move(public_path('storage/images/'), $image_full_name);
                $image[] = $image_full_name;
                $titled      = substr($image_full_name, 0, strrpos($image_full_name, "."));
            }

            $new_image = new Image();

            $new_image->category_id = $request->category;
            $new_image->title       = $titled;
            $new_image->image      = explode('|', $image);

            auth()->user()->images()->save($new_image);

            Session::flash('add-section','Data telah ditambahkan!');
            return redirect()->route('media.index');
        }
    }

Anyone can help me, please.

0 likes
8 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Move the storing of images in the database inside the foreach

foreach ($request->file('images') as $imagefile) {
               $image_name  = md5(rand(1000, 10000));
               $ext = strtolower($imagefile->getClientOriginalExtension());
               $image_full_name = $image_name.'.'.$ext;
               $imagefile->move(public_path('storage/images/'), $image_full_name);
                
                $titled      = substr($image_full_name, 0, strrpos($image_full_name, "."));

            $new_image = new Image();

            $new_image->category_id = $request->category;
            $new_image->title       = $titled;
            $new_image->image      = $image_full_name;

            auth()->user()->images()->save($new_image);
            }
1 like
nurularifin's avatar

@Sinnbeck OMG how can you be so smart, thank u so much sir, so simple but my brain can't think as simple as that.

Sinnbeck's avatar

@nurularifin Hehe thanks. Now you know this too. So next time you see a similar problem, you have a solution.

1 like
nurularifin's avatar

@SilenceBringer in my table I have several fields and two of them are called IMAGE and TITLE, IMAGE to store the name of the image include extension and title only to stores image name without extension.

SilenceBringer's avatar

@nurularifin I understand. But look: here

$image_name  = md5(rand(1000, 10000));

you generate image name. then append extension

$image_full_name = $image_name.'.'.$ext;

and then just remove extension

                $titled      = substr($image_full_name, 0, strrpos($image_full_name, "."));

so, really your $titles matches $image_name

nurularifin's avatar

@SilenceBringer as the result yes it is sir, $image_name just for generate uniq name then $ext to take extension like JPG, PNG and so on, if $image_name combine with $ext will be like this 21389712873982j.PNG, then this part $titled = substr($image_full_name, 0, strrpos($image_full_name, ".")); will output like this 21389712873982j. That's it sir.

Please or to participate in this conversation.