Today, i will share with you How to done multiple files upload with laravel validation with example. single file upload is very easy to in laravel, but multiple files upload is bit harder for laravel learner. so, in this article i will share with you how to done multiple files upload with validation steps by steps.
We all are know files upload is a very important part in any web application. because most of application has required files uploading functionality. in this article we will see how to done it and also see some laravel files uploading validation like max file size, min file size, file type, etc...
Just follow the below steps and you can easily upload multiple files with validation in laravel application:
Step - 1 : Create Laravel Fresh New Project
Step - 2 : Setup Database Credentials
Step - 3 : Create Migration & Model
Step - 4 : Create Route
Step - 5 : Create Controller
Step - 6 : Create Blade View
Step - 1 : Create Laravel fresh New Project
First, we need to create one fresh laravel application help of composet command. just run the following command in your terminal and create one laravel application.
composer create-project --prefer-dist laravel/laravel imageupload
Step - 2 : Configure Database Setting.
Now, into the second step we should configure database setting in .env
file just open your .env
file and make the following changes. set youe database name, username and password.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=root
DB_PASSWORD=root
Step - 3 : Create Migration & Model
Now, we need to create migration for transactions
table. run the following command in your terminal.
php artisan make:migration create_documents
After run this command, then open that created file which will created on database/migrations
folder. just open it and put the following code into that migration file.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDocuments extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('documents', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('file');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('documents');
}
}
Next, run the php artisan migrate
command in the terminal for create documents
table.
After, run the migration then we need to create Document
model for store files data. run the following command in the terminal for create model.
php artisan make:model Document
Now open the app/Document.php
file and write the following code into it.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Document extends Model
{
protected $table = 'documents';
protected $guarded = array();
public function storeData($input)
{
return static::create($input);
}
}
Step - 4 : Create Routes
We will create two routes in the web.php file. Go to app/routes/web.php
file and create two below routes here :
Route::get('document', '[email protected]')->name('document');
Route::post('documentstore', '[email protected]')->name('documentstore');
Step - 5 : Create Controller
Now, we need to create DocumentController.php
file in app\Http\Controllers
folder. just run the following command in your terminal and create your controller.
php artisan make:controller DocumentController
Just, open your DocumentController.php
file and write the following code into this controller.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator,Redirect,Response,File;
Use App\Document;
class DocumentController extends Controller
{
public function index()
{
return view('document');
}
public function store(Request $request)
{
request()->validate([
'file' => 'required',
'file.*' => 'mimes:doc,pdf,docx,txt,zip,jpeg,jpg,png'
]);
if($request->hasfile('file')) {
foreach($request->file('file') as $file)
{
$fileName = time().rand(0, 1000).pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$fileName = $fileName.'.'.$file->getClientOriginalExtension();
$file->move(public_path(),$fileName);
$input['file'] = $filename;
Document::create($input);
}
}
\Session::put('success', 'Document will be uploaded.');
return redirect()->route('document');
}
}
Step - 6 : Create Blade View
Now, the last step we need to create one simple HTML blade file in resources/views/document.blade.php
file and write the following code into it.
<html lang="en" class="">
<head>
<meta charset="UTF-8">
<title>Laravel 6 - Multiple Files upload with Validation Example.</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="card">
<div class="card-header">Laravel Upload File Example</div>
<div class="card-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.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('documentstore') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" class="form-control-file" name="file[]" multiple>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</html>
Hope you like this tutorials......
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Login with Twitter in Angular9 using Firebase
I am going to show you how you can ...PHP Check if string is valid url Code Example
When I was working with one Payment gate...Laravel Eloquent Serialization to array and Json
When working with API in Laravel or simi...PHP7 - Login Logout System in PHP with Session
In this tutorial, we are going to learn...How to convert URL to array in PHP
An URL is the string of many data. URL c...