In this example, I will show you laravel 9 multiple file upload example. In this article, we will implement a laravel 9 multiple files upload. We will look at examples of multiple file upload laravel 9. it's a simple example of laravel 9 multiple file upload with preview. follow the below step for uploading multiple files in laravel 9.
Here, we will install laravel 9 and create a simple form with a file input field that you can use to select multiple files. after submitting the form we will store those files in a folder and database.
So, let's follow the below step to create multiple files upload in the laravel 9 application example.
Step 1: Install Laravel 9
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; return new class extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('files', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('files'); }};
Next, run create new migration using laravel migration command as bellow:
php artisan migrate
Now we will create File model by using following command:
php artisan make:model File
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' ];}
In this step, we will create a new FileController; in this file, we will add two method index() and store() for render view and store files into folder and database logic.
Let's create FileController by following command:
php artisan make:controller FileController
next, let's update the following code to Controller File.
app/Http/Controllers/FileController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use App\Models\File; class FileController extends Controller{ /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('fileUpload'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function store(Request $request) { $request->validate([ 'files' => 'required', 'files.*' => 'required|mimes:pdf,xlx,csv|max:2048', ]); $files = []; if ($request->file('files')){ foreach($request->file('files') as $key => $file) { $fileName = time().rand(1,99).'.'.$file->extension(); $file->move(public_path('uploads'), $fileName); $files[]['name'] = $fileName; } } foreach ($files as $key => $file) { File::create($file); } return back() ->with('success','You have successfully upload file.'); }}
Furthermore, open routes/web.php file and add the routes to manage GET and POST requests for render view and store files logic.
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\FileController; /* |--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/ Route::controller(FileController::class)->group(function(){ Route::get('file-upload', 'index'); Route::post('file-upload', 'store')->name('file.store');});
Step 5: Create Blade File
At last step we need to create fileUpload.blade.php file and in this file we will create form with file input button. So copy bellow and put on that file.