rabintamu's avatar

Upload Multiple Product Images

I am still learning laravel. I want to upload multiple images for my products but with this code i can upload only one image, help me to upload multiple images with this code.

public function store(Request $request)

{
    $details = request()->validate([
        'image' => ['required', 'image'],
        'title' => 'required',
        'price' => 'required',
        'description' => 'required',
        'location' => 'required',
    ]);


    $imagePath = request('image')->store('uploads','public');
    $image = Image::make(public_path("storage/{$imagePath}"))->fit(1200,1200);
     $image->save();


  auth()->user()->posts()->create([
    'title' => $details['title'],
    'price' => $details['price'],
    'description' => $details['description'],
    'location' => $details['location'],
      'image' => $imagePath
  ]);


    return redirect('/profile/' . auth()->user()->id);
}
0 likes
2 replies
Neven's avatar

Hi, For this you need to add the parameter multiple on the input['type:file'] in your form for accept multiple files.

{{ Form::file('images[]',['class' =>'your-class','multiple']) }}

Then in your method store you will receive the files in $request->images as a collection. So you just need to work this collection of files as if you work with one image. But in a foreach loop.

foreach($request->images as $image){
    //bla bla bla
}
adityakunhare's avatar

Try this, working for me:

    $customer = Customers::create($this->validateRequest());

    $customer->update([
        'image1'=> request()->image1->store('uploads','public'),
        'image2'=> request()->image2->store('uploads','public'),
    ]);

Please or to participate in this conversation.