Laravel 6 – Multiple Files upload with Validation Example
In this post we will give you information about Laravel 6 – Multiple Files upload with Validation Example. Hear we will give you detail about Laravel 6 – Multiple Files upload with Validation ExampleAnd how to use it also give you demo for it if it is necessary.
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 into that migration file.
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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 into it.
namespace App;
use IlluminateDatabaseEloquentModel;
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', 'DocumentController@index')->name('document');
Route::post('documentstore', 'DocumentController@store')->name('documentstore');
Step – 5 : Create Controller
Now, we need to create DocumentController.php file in appHttpControllers 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 into this controller.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Validator,Redirect,Response,File;
Use AppDocument;
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 into it.
<html lang="en" >
<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 >
<div >
<div >
<div >Laravel Upload File Example</div>
<div >
@if ($message = Session::get('success'))
<div >
<button type="button" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
@endif
@if (count($errors) > 0)
<div >
<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 >
<input type="file" name="file[]" multiple>
</div>
<button type="submit" >Submit</button>
</form>
</div>
</div>
</div>
</div>
</html>
Hope you like this tutorials……
Hope this and post will helped you for implement Laravel 6 – Multiple Files upload with Validation Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs
