Desssha's avatar

HOW Upload Multiple Image FROM MULTIPLE INPUT

i try to upload multi image but it not work not save image in path . if upolad only one image from only one input it work . but multiple not . i used this way also not work html

Image One(Main): *
            <div class="col-lg-4">
                <div class="form-group mg-b-10-force">
                    <label class="form-control-label">Image 2: <span class="tx-danger">*</span></label>
                    <label class="custom-file">
                        <input type="file" name="image_2" id="file" class="custom-file-input" >
                        <span class="custom-file-control custom-file-control-primary"></span>
                    </label>
                </div>
            </div><!-- col-4 -->

            <div class="col-lg-4">
                <div class="form-group mg-b-10-force">
                    <label class="form-control-label">Image 3: <span class="tx-danger">*</span></label>
                    <label class="custom-file">
                        <input type="file" name="image_3" id="file" class="custom-file-input"  >
                        <span class="custom-file-control custom-file-control-primary"></span>
                    </label>
                </div>
            </div><!-- col-4 -->

and it my repository

public function storeProducts($request){

    $products = new Product();

    $products['name'] = $request->name;
    $products['code'] = $request->code;
    $products['discount_price'] = $request->discount_price;
    $products['quantity'] = $request->quantity;
    $products['size'] = $request->size;
    $products['color'] = $request->color;
    $products['price'] = $request->price;
    $products['category_id'] = $request->category_id;
    $products['subcategory_id'] = $request->subcategory_id;
    $products['brand_id'] = $request->brand_id;
    $products['video'] = $request->video;
    $products['desc'] = $request->desc;
    $products['main_slider'] = $request->main_slider;
    $products['mid_slider'] = $request->mid_slider;
    $products['hot_deal'] = $request->hot_deal;
    $products['hot_new']  = $request->hot_new;
    $products['best_rate']  = $request->best_rate;
    $products['trend']  = $request->trend;
    $products['status']  = 1;

    $image_1  = $request->image_1;
    $image_2  = $request->image_2;
    $image_3  =  $request->image_3;



    if ($request->file('image_1','image_2','image_3')){
        foreach($request->file('image_1','image_2','image_3') as $image) {
            $file = $request->file('image_1' , 'image_2' , 'image_3');
            $filename = date('YmdHi') . $file->getClientOriginalName();
            $image->move(public_path('upload/product'), $filename);
            $products[] = $filename;
        }

    }


        $products->save();

       // $product = DB::table('products')->insert($data);

        $notificat = array(
            'message' => 'product added Successfully',
            'alert-type' => 'success'
        );
        return redirect()->back()->with($notificat);
    }
0 likes
2 replies
geowrgetudor's avatar

Why do you use "image_1", "image_2" etc? You should use array "image[]" as in:

image 1:
<input type="file" name="image[]" id="file" class="custom-file-input" >

image: 2
<input type="file" name="image[]" id="file" class="custom-file-input" >

image: 3
<input type="file" name="image[]" id="file" class="custom-file-input" >

So you can do it like this

    if ($request->file('image')){
        foreach($request->file('image') as $image) {
            $filename = date('YmdHi') . $image->getClientOriginalName();
            $image->move(public_path('upload/product'), $filename);
            $products[] = $filename;
        }
    }

Also you should use request validation https://laravel.com/docs/8.x/validation When using request validations, make sure that the requested fields' names are the same as your model's table columns. That way you can easily create/update by passing the validated data, which is an array.

So, using that, you can reduce that big chunk of code to this:

$data = $request->validated();
$product = Product::create($data);

foreach($data['image'] as $image) {
				$filename = date('YmdHi') . $image->getClientOriginalName();
				$image->move(public_path('upload/product'), $filename);
				// Do something with your image here (maybe store it in another table)
}

return redirect()->back();

Some easy way to attach images to a model is to use this package from Spatie https://github.com/spatie/laravel-medialibrary which does everything for you.

Snapey's avatar

To integrate with this community PLEASE DONT SHOUT in your question, and make sure you format your code blocks.

Please or to participate in this conversation.