Multiple file upload in Laravel 9 with example

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

				
					<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
class CreateFilesTable extends Migration
{
    public function up()
    {
        Schema::create('files', function (Blueprint $table) {
            $table->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

				
					<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 
class File extends Model
{
    use HasFactory;
 
    protected $fillable = [
        'name'
    ];
}
				
			

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
				
					<?php
 
return [
    'doc'  => 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
				
					<?php
 
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MultipleFileUploadController;
 
Route::get('multiple-file-upload', [ MultipleFileUploadController::class, 'getFileUploadForm' ])->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.
				
					<!DOCTYPE html>
<html>
   <head>
      <title>Laravel 9 Multiple File Upload Tutorial With Example DAILYWEBDESIGN</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <div class="panel panel-primary">
            <div class="panel-heading mb-3 mt-5">
               <h2>Laravel 9 Multiple File Upload Tutorial With Example - ScratchCode.io</h2>
            </div>
            <div class="panel-body">
               @if ($message = Session::get('success'))
                   <div class="alert alert-success alert-block">
                      <button type="button" class="close" data-dismiss="alert">×</button>
                      <strong>{{ $message }}</strong>
                   </div>
               @endif
 
               @if (count($errors) > 0)
               <div class="alert alert-danger">
                  <strong>Whoops!</strong> There were some problems with your input.
                  <ul>
                     @foreach ($errors->all() as $error)
                     <li>{{ $error }}</li>
                     @endforeach
                  </ul>
               </div>
               @endif
 
               <form action="{{ route('store.multiple-files') }}" method="POST" enctype="multipart/form-data">
                  @csrf
                  <div class="row">
                     <div class="mb-3 col-md-6">
                        <label class="form-label">Select Files:</label>
                        <input type="file" name="documents[]" class="form-control" multiple/>
                     </div>
 
                     <div class="col-md-12">
                        <button type="submit" class="btn btn-success">Upload Files...</button>
                     </div>
                  </div>
               </form>
            </div>
         </div>
      </div>
   </body>
</html>
				
			

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top