Multiple Images Upload Tutorial with Laravel 6.0

Upload Multiple Images in Laravel 6.0,Laravel 6.0 Multiple Images File Upload, Upload Multiple Images and Files with Validation in Laravel 6.0

Hello friends, today I will tell you through this tutorial how do you upload multiple image files through Laravel 6.0. So we will learn step-to-step in Laravel 6.0.

Step 1: Laravel 6.0 Install
Step 2: Create Table in Laravel 6.0
Step 3: Create Route in Laravel 6.0
Step 4: Create Model in Laravel 6.0 for Multiple Images Upload
Step 5: Create Controller in Laravel 6.0
Step 6: Create Blade Files in Laravel 6.0 For Multiple Images File Upload

Step 1: Laravel 6.0 Install

 

The first step is to install laravel 6.0. To install laravel 6.0 we first have to install the composer. If you have already installed the composer in your system or laptop. There is no need to install a composer.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Table in Laravel 6.0 for multiple images Upload

 

This is the third step. In this step you will be told how to create a table in laravel 6.0 for multiple file upload, it is very easy to create table in laravel 6.0. The table will create.

php artisan make:migration create_images_table --create=images

database/migrations

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateImagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('file');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('images');
}
}

after that Now you have to run this migration by following command:

php artisan migrate

Step 3: Create Route in Laravel 6.0

 

In this step, when we select the file, submit the data by post method in laravel 6.0. So in the web.php file, the url of post method will be given in this way. And add this url to the controller.

Route::get('multipleimage', function () {
    return view('multipleimage');
});
Route::post('multiplefileupload', 'imageController@multiplefileupload');

Step 4: Create Model in Laravel 6.0

 

Then after that we will create a database table model in laravel 6.0. To create a model we can create a model by using the laravel 6.0 command. The systax to create the model is given below.

php artisan make:model Image

app/Blog.php

<?php 
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
protected $fillable = [
'file'
];
}

 

Step 5: Create Controller in Laravel 6.0

 

Now we will create this one controller and keep this controller’s name as imageController.php. Then after that we will write the code to upload multiple image file in these controller. How to upload multiple image files in Laravel 6.0 You can see the code in this controller.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use Validator;
use Redirect;
use View;
class imageController extends Controller
{
public function multiplefileupload(Request $request)
{
$images=array();
if($files=$request->file('file')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
/*Insert your data*/
DB::table('images')->insert([
'file' => $name
]);
/*Insert your data*/
}
}
return redirect()->back()->with('message', 'Successfully Save Your Image file.');
}
}

Step 6: Create Blade Files in Laravel 6.0

 

<!DOCTYPE html>
<html>
<head>
<title>Laravel 6.0 Multiple Image File Upload</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
</head>
<body>
<form action="{{url('multiplefileupload')}}" enctype="multipart/form-data" method="post">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<div class="col-md-12">
<div class="form-group">
<label for="">Multiple File Select</label>
<input required type="file" class="form-control" name="file[]" multiple>
</div>
</div>
<div class="col-md-6">
<div class="box-footer">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</div>
</form>
</body>
</html>