How to upload multiple Image in Laravel 6

How to upload multiple Image in Laravel 6

In this tutorial, you'll learn how to multiple image upload in Laravel 6. We will upload multiple image with validation in Laravel 6. We will use laravel image validation and store to folder and database for Laravel 6 multiple image upload.

Originally published at https://itsolutionstuff.com

We will create simple multiple image upload in Laravel 6. So you basically use this code on your Laravel 6 application.

We are going from starch so, we will upload multiple file and store on server then after we will store database too. So in this example we will create "files" table using Laravel migration and write code for route, controller and view step by step.

So here you have to just follow bellow step to create Laravel 6 multiple File Upload with Validation Example.

Step 1: Download Laravel 6

In the first step, we will download a new simple copy source code of Laravel App project by typing the some following command.

composer create-project --prefer-dist laravel/laravel blog
Step 2: Add Migration and Model

Here, we need create database migration for files table and also we will create model for files table.

php artisan make:migration create_files_table

Migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFormsTable extends Migration
{
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->increments('id');
$table->string('filenames');
$table->timestamps();
});
}

public function down()
{
    Schema::dropIfExists('files');
}

}
php artisan migrate

now we will create File model by using following command:

php artisan make:model File

Read Also: Laravel 6 CRUD Application Tutorial

Step 3: Create Routes

In third step, we will create routes for multiple file upload. so create two route with GET and POST route example.

routes/web.php

Route::get('file','[email protected]');
Route::post('file','[email protected]');
Step 4: Create Controller

Now we require to add new FileController controller for manage route So let's create FileController with create and store method. Make sure you need to create "files" folder in your public directory.

app/Http/Controllers/FileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('create');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{

    $this-&gt;validate($request, [
            'filenames' =&gt; 'required',
            'filenames.*' =&gt; 'mimes:doc,pdf,docx,zip'
    ]);

    if($request-&gt;hasfile('filenames'))
     {
        foreach($request-&gt;file('filenames') as $file)
        {
            $name = time().'.'.$file-&gt;extension();
            $file-&gt;move(public_path().'/files/', $name);  
            $data[] = $name;  
        }
     }

     $file= new File();
     $file-&gt;filenames=json_encode($data);
     $file-&gt;save();

    return back()-&gt;with('success', 'Data Your files has been successfully added');
}

}

Step 5: Create Blade File

in this step we need to create create.blade.php file in resources folder. So let's create file:

resources/views/create.blade.php

Read Also: Laravel 6 Ajax CRUD Tutorial

<html lang="en">
<head>
<title>Laravel 6 Multiple File Upload Example</title>
<script src="jquery/1.9.1/jquery.js"></script>
<link rel="stylesheet" href="3.3.6/css/bootstrap.min.css">
</head>
<body>

<div class="container lst">

@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Sorry!</strong> There were more problems with your HTML input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif

<h3 class="well">Laravel 6 Multiple File Upload</h3>
<form method="post" action="{{url('file')}}" enctype="multipart/form-data">
{{csrf_field()}}

&lt;div class="input-group hdtuto control-group lst increment" &gt;
  &lt;input type="file" name="filenames[]" class="myfrm form-control"&gt;
  &lt;div class="input-group-btn"&gt; 
    &lt;button class="btn btn-success" type="button"&gt;&lt;i class="fldemo glyphicon glyphicon-plus"&gt;&lt;/i&gt;Add&lt;/button&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class="clone hide"&gt;
  &lt;div class="hdtuto control-group lst input-group" style="margin-top:10px"&gt;
    &lt;input type="file" name="filenames[]" class="myfrm form-control"&gt;
    &lt;div class="input-group-btn"&gt; 
      &lt;button class="btn btn-danger" type="button"&gt;&lt;i class="fldemo glyphicon glyphicon-remove"&gt;&lt;/i&gt; Remove&lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;button type="submit" class="btn btn-success" style="margin-top:10px"&gt;Submit&lt;/button&gt;

</form>
</div>

<script type="text/javascript">
$(document).ready(function() {
$(".btn-success").click(function(){
var lsthmtl = $(".clone").html();
$(".increment").after(lsthmtl);
});
$("body").on("click",".btn-danger",function(){
$(this).parents(".hdtuto control-group lst").remove();
});
});
</script>

</body>
</html>

Now you can simple run and check it.

I hope it can help you...

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

What’s New in Laravel 6.0

Laravel 6 CRUD Application Tutorial

Laravel 6 Image Upload Tutorial

Laravel 6 Authentication Tutorial


Laravel Framework Development Services | Laravel Web Development

CMARIX is leading Laravel Web application Development Service Provider with the track record of implementing diverse high-performance Laravel web applications with skilled Laravel developer team for clients across the globe<a href="https://www.cmarix.com/laravel-framework-development-services.html?utm_source=SB" target="_blank">.Continue</a>

CMARIX is leading Laravel Web application Development Service Provider with the track record of implementing diverse high-performance Laravel web applications with skilled Laravel developer team for clients across the globe.Continue

Which is the best PHP Web Development Company in India?

Which is the best PHP Web Development Company in India?

Many experienced players prefer to give it a go ahead with PHP for their web development needs because of the simplicity that PHP provides. PHP is loaded with features that many other languages lack and those pre-defined features can bring new functionalities and finishing touch to your existing or new website that you are looking forward to create. Wouldn’t it be great to get the best PHP Web Development Company for your project; we have a reputation as few of the leading firms in IT industry when it comes to PHP development.

Perks you should keep an eye out for before considering to outsource project to any web development company:

1. Company’s Reputation- It is very important to know about the previous projects that the company has delivered and the success rate of those products. You can visit company’s website for client testimonials or can google reviews given to them by genuine clients. We follow transparency, you can contact our previous clients and let them tell you how their experience was while working with us.

2. Technical Skill set and Expertise- You should select a company that has multiple resource pool of expert and technically sound developers. Our developers are experienced with different PHP frameworks and are exceptionally good with writing bug free codes. You can also hire PHP developer in India from us, as our developers have on hand experience in PHP development (core and frameworks)

3. The Budget- Costing has always played as one of the most important roles in every business deal. We provide competitive pricing with no hidden cost guaranteed. With us you can expect your project to be in best quality with no compromise to be made even with the best price in the market.

Why Data EximIT is the best PHP web development company in India?

Here are some of the features that might love about Data EximIT:

 Reputation of Data EximIT is wonderful among the previous clients.

 We have around 87% of retention ratio of clients.

 We were time committed, means every milestone was achieved before or on time.

 Data EximIT follows Agile development so it was the best possible methodology.

 We have huge team with different expertise over multiple PHP frameworks, so you are provided with ample to options to choose from.

 We are very active in communication part. Method or time of communication was never an issue at Data EximIT.

 We have some of the best designers to ensure that your project not only functions but also looks and feels fabulous.

 We provided customers with best costing without compromising on the quality at all.

 We also provide dedicated PHP developer or team of PHP developers on hire for your project.

Data EximIT is where customers feel confident to come up with idea and leave with fully functioning & best in quality projects. You can also avail our developers at your requested site location if you decide to hire PHP developers in India from us.

Feel free to contact our experts and let success reach you by working with the best PHP web development company.

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company