0

i have this code in my controller that can make me upload successfully just one file , and i want to upload many files in once time :

public function store(Request $request, $id) {
    $request->validate([
            'image' => 'required',
    ]);

    $listing = Listing::findOrFail($id);
    $image = new Listingimage();

    if ($request->hasFile('image')) {
        $file = $request->file('image');
        $extention = $file->getClientOriginalExtension();
        $filename = time() . '.' . $extention;
        $file->move('assets/images/listingimages/', $filename);
        $fileOriginalName = $file->getClientOriginalName();
    }

    $image->listing_id = $id;
    $image->image_url = $filename;
    $image->nom_image = $fileOriginalName;
    $image->save();
    return redirect()->back();
}

i use also this input :

<form action="{{ route('Listingimages.store', $listing->id) }}" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="_method" value="PUT" />
    {{csrf_field()}}
    {{method_field('PUT')}}
    <label>  Insert image</label>
    <input type="file" name="image" id="files" class="form-control" multiple>

so , how can i upload many files in once time ?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
imane
  • 55
  • 3
  • 9
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Nov 10 '21 at 07:56
  • 1
    have you searched online? the `$request->file('image')` will return an array. foreach that array. – Gert B. Nov 10 '21 at 08:00
  • 1
    Does this answer your question? [Laravel 5.3 multiple file uploads](https://stackoverflow.com/questions/39846148/laravel-5-3-multiple-file-uploads) – SEYED BABAK ASHRAFI Nov 10 '21 at 08:28
  • Please try searching before posting a new question, there are so many duplicates here already: https://stackoverflow.com/questions/66131518/upload-multiple-files-in-laravel, https://stackoverflow.com/questions/56108388/laravel-upload-multiple-files, https://stackoverflow.com/questions/58080264/laravel-how-to-do-multiple-file-upload-and-storing-data-in-two-tables, https://stackoverflow.com/questions/49154444/uploading-multiple-files-in-laravel-5/49154637, https://stackoverflow.com/questions/69806619/how-to-upload-multiple-files-in-database-using-laravel ... – Don't Panic Nov 10 '21 at 13:34

1 Answers1

1

change input name

<input type="file" name="image[]" id="files" class="form-control">

controller

   public function store(Request $request, $id) {
        $request->validate([
            'image' => 'required',
        ]);

        $listing = Listing::findOrFail($id);
        if ($request->hasFile('image')) {
            foreach($request->file('image') as $file)
            {
                $image = new Listingimage();
                $file = $request->file('image');
                $extention = $file->getClientOriginalExtension();
                $filename = time() . '.' . $extention;
                $file->move('assets/images/listingimages/', $filename);
                $fileOriginalName = $file->getClientOriginalName();
                $image->listing_id = $id;
                $image->image_url = $filename;
                $image->nom_image = $fileOriginalName;
                $image->save();
            }

        }
        return redirect()->back();

    }