Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jhyaps's avatar

Multiple files upload in Laravel 9

Hello,

I am having a problem storing multiple files in a Laravel. I have created a function to store , where my files will store to the directory but it cannot save to the database.

My Request

$rules =  [
            'file_name.*' => 'mimes:pdf,docx,jpg,png|nullable',
            'file_type_id.*' => 'required_with:file_name.*,pdf,docx,jpg,png',
            
        ];

my Store Controller

public function store(SupplierRequest $request)
    {

        $data = $this->handleRequest($request);

        DB::beginTransaction();

        try {

            $supplier = Supplier::create($data);

            foreach ($data['file_name'] as $key => $file) {

                $value['supplier_id'] = $supplier->id;
                $value['file_name'] = $file;
                $value['file_type_id'] = $data['file_type_id'][$key];

                SupplierFile::create($value);

            }

            DB::commit();

            session()->flash('success', __('message.supplier-store'));
            return response()->json(['success' => __('message.supplier-store')]);

        } catch (\Throwable$th) {

            DB::rollback();
            return redirect(app()->getlocale() . '/supplier')->with('error', $th->getMessage());
        }
    }

My handlefunction()

private function handleRequest($request)
    {

        $data = $request->all();

        $data['file_name'] = [];
        if ($request->hasFile('file_name')) {

            $supplier_file = $request->file('file_name');

            foreach ($supplier_file as $files) {

                $filename = $files->getClientOriginalName();

                $destination = public_path(config('inventory.supplier_files.directory'));

                $successUploaded = $files->move($destination, $filename);

                $data['file_name'][] = $filename;

            } 

        }
        return $data;

    }

Though files will be save to the directory but it will not save to the database why ?

0 likes
5 replies
LaryAI's avatar
Level 58

It looks like you are missing the $value['file_name'] = $file; line in your foreach loop. This line is necessary to save the file name to the database.

Try adding the following line to your foreach loop:

$value['file_name'] = $file;

This should save the file name to the database.

jhyaps's avatar

@LaryAI

I have added that already, look in to the question

foreach ($data['file_name'] as $key => $file) {

                $value['supplier_id'] = $supplier->id;
                $value['file_name'] = $file; //here it is 
                $value['file_type_id'] = $data['file_type_id'][$key];

                SupplierFile::create($value);

            }
Snapey's avatar

your code is difficult to follow and needlessly complicated , which is perhaps why there are no suggestions

does ANYTHING get created in the database?

jhyaps's avatar

@Snapey . Actually yes, with this code i can store my suppliers details in database, if i comment the foreach loop , but if i uncomment the loop, data will not save into the database,

here, i am using a DB::beginTransaction(); try catch , this will prevent me saving the data in database if errors is there in a code.

your code is difficult to follow and needlessly complicated

Is that much complicated ??

i am trying to upload a multiple files as per their type for a suppliers,

firstly in the store method, i save my data to related table like for details i will save to the suppliers_table and for the files, i will save to the suppliers_files table.

for the files i used a handle function because this function is needed to both store and update method. where in this method all the files will save to the directory.

Please or to participate in this conversation.