Hello Devs,
In this tutorial, we are going to learn multiple file upload with validation in laravel 7.
Follow this step by step guide given below:
Step 1 :
Install Laravel App
composer create-project --prefer-dist laravel/laravel Blog
Step 2 :
Setup Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here
Step 3 :
Generate Migration & Model
php artisan make:model File -m
Run this command:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatedocumentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->increments('id');
$table->string('file');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('documents');
}
}
Run this following command:
php artisan migrate
app/File.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'file'
];
}
Step 4 :
Create Route
Route::get('file', 'MultipleFileController@index');
Route::post('save', 'MultipleFileController@save')->name('file.save');
Step 5 :
Create Controller
php artisan make:controller MultipleFileController
app/controllers/MultipleFileController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\File;
class MultipleFileController extends Controller
{
public function index()
{
return view('multiplefile');
}
public function save(Request $request)
{
request()->validate([
'file' => 'required',
]);
if($request->hasfile('file'))
{
foreach($request->file('file') as $file)
{
$filename=$file->getClientOriginalName();
$file->move(public_path().'/upload/', $filename);
$insert['file'] = "$filename";
}
}
File::create($insert);
return back()->withSuccess('Great! files has been successfully uploaded.');
}
}
Step 6 :
Create Blade view
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 7 - Multiple File Upload With Validation - Rathorji.in</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script></head>
<body class="bg-dark">
<div class="container">
<div class="row">
<div class="col-md-6 mt-3 offset-md-3">
<div class="card">
<div class="card-header">
<h6>Laravel 7 - Multiple File Upload With Validation - Rathorji.in</h6>
</div>
<div class="card-body">
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block alert-dismissible">
<button type="button" class="close" dlta-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
@endif
@if (count($errors) > 0)
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" dlta-dismiss="alert">×</button>
@foreach ($errors->all() as $error)
{{ $error }}<br>
@endforeach
</div>
@endif
<form action="{{ route('file.save')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" class="form-control" name="file[]" id="file" multiple='multiple' aria-describedby="fileHelp">
</div>
<button type="submit" class="btn btn-success btn-sm">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
</html>
Run this command:
php artisan serve
Open this URL:
http://localhost:8000/file
I hope this example helps you.