In this article, we will show you how to upload file in laravel 8. This article will give you simple example of file upload in laravel 8. you will learn file upload in laravel 8.
I will provide you with full example for the way to file upload come in laravel eight. So, let’s follow few step to form example of laravel eight file transfer tutorial.
Step 1 :- Install Laravel 8
run command to install fresh laravel project
composer create-project --prefer-dist laravel/laravel blog
Step 2 :- Database Configuration
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=laravel@123
Step 3 :- Create Migration, Model and Controller
Create Model
php artisan make:model File -m
Add code in database/migrations/create_files_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateFilesTable 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'); } }
Add File table values in 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; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', ]; }
Migrate the database using the command
php artisan migrate
Create File Controller
php artisan make:controller FileUploadController
open FileUploadController go to app\Http\Controllers\FileUploadController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\File; class FileUploadController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function fileUpload() { return view('fileUpload'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function fileUploadPost(Request $request) { $request->validate([ 'file' => 'required|file|mimes:jpg,jpeg,bmp,png,doc,docx,csv,rtf,xlsx,xls,txt,pdf,zip', ]); $fileName = time().'.'.$request->file->extension(); $request->file->move(public_path('file'), $fileName); /* Store $fileName name in DATABASE from HERE */ File::create(['name' => $fileName]) return back() ->with('success','You have successfully file uplaod.') ->with('file',$fileName); } }
Add Routes in Web.php
Routes/web.php
<?php use App\Http\Controllers\FileUploadController; Route::get('file-upload', [ FileUploadController::class, 'fileUpload' ])->name('file.upload'); Route::post('file-upload', [ FileUploadController::class, 'fileUploadPost' ])->name('file.upload.post');
Step 4:- Add Blade File
So let’s just create following file and put bellow code.
resources/views/fileUpload.blade.php
resources/views/fileUpload.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 8 File Upload - CodePlaners.com</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> </head> <style type="text/css"> h2{ text-align: center; font-size:22px; margin-bottom:50px; } body{ background:#f2f2f2; } .section{ margin-top:150px; padding:50px; background:#fff; } </style> <body> <div class="container"> <div class="col-md-8 section offset-md-2"> <div class="panel panel-primary"> <div class="panel-heading"> <h2>Laravel 8 file Upload - CodePlaners.com</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 your input. <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-md-10"> <input type="file" name="file" class="form-control"> </div> <div class="col-md-2"> <button type="submit" class="btn btn-success">Upload</button> </div> </div> </form> </div> </div> </div> </div> </body> </html>
Now we are ready to run file upload with laravel 8 so run command for quick run
php artisan serve