This post will demonstrate multiple file uploading using an example from the Laravel 9 tutorial. The Laravel 9 multiple file upload allows us to effortlessly upload several file types such as.pdf,.doc,.docx,.csv,.txt,.png,.gif,.zip, and so on. We’ll add validation rules to Laravel so that only specific file formats are allowed. Now let’s get started.
Step 1: Install Laravel 9
You can skip this step if Laravel 9 is already installed locally on your computer. By entering the following command into your terminal, you can rapidly install the most recent version of Laravel 9. We’ll call it demo-app in this instance, but you may call it anything you want.
composer create-project --prefer-dist laravel/laravel demo-app
Step 2: Create Migration
cd demo-app
php artisan make:migration create_files_table --create=files
After running the above command a new migration file will be created in “database/migrations” directory.
database/migrations/2024_08_07_172759_create_files_table.php
id();
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('files');
}
}
Now, run the below command to create database tables.
php artisan migrate
Step 3: Create A Model
This stage involves creating a file model, which enables us to connect to the file table and use the model to execute database operations. To construct a model, execute the command below.
php artisan make:model File
Then the new file will be created in demo-app/app/Models/File.php. In the model add the name field in the fillable array.
app/Models/File.php
Step 4: Create MultipleFileUploadController
Let’s create a MultipleFileUploadController and let’s add those two methods that we have added in the routes file getFileUploadForm() and store().
app/Http/Controllers/MultipleFileUploadController.php
Notes: To validate .doc, .docx, .xlsx, .xls you need to create mimes.php in config directory and add the following content.
config/mimes.php
array('application/msword', 'application/vnd.ms-office'),
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
'xls' => array('application/vnd.ms-excel'),
];
Step 5: Create Routes
Now, let’s add the Laravel resource routes in the routes/web.php
name('get.multipleFileupload');
Route::post('multiple-file-upload', [ MultipleFileUploadController::class, 'store' ])->name('store.multiple-files');
Step 6: Create Blade/HTML File
Finally, we need to create a file called multiple-file-upload.blade.php in the views folder. We will add the HTML for the multiple-file upload form to this file.
Laravel 9 Multiple File Upload Tutorial With Example DAILYWEBDESIGN
Laravel 9 Multiple File Upload Tutorial With Example - ScratchCode.io
@if ($message = Session::get('success'))
{{ $message }}
@endif
@if (count($errors) > 0)
Whoops! There were some problems with your input.
@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach
@endif
Step 7: Output – Laravel 9 Multiple File Upload With Example
Joy! All the instructions for the Laravel 9 multiple files upload tutorial with an example have been finished. Let’s try the command below to see how it functions.
php artisan serve
That’s it from our end. We hope this article helped you with the Laravel 9 Multiple File Upload.