Multiple Image Upload In Laravel App

Multiple Image Upload In Laravel App

In this tutorial, you will learn how to multiple image upload in laravel from scratch. This tutorial, you will show you how to upload multiple image in laravel application step by step simple and easy way.

Also you will upload multiple images with validate image type in laravel app. you will also validate image with laravel validator and store the images to database and folder. And this example also work with laravel 7.x, 6.x, 5 version.

In this tutorial, you will tell you each things about multiple image upload in laravel app. Just follow few steps and upload image in folder laravel app .

Multiple Image Upload in Laravel

Just follow the below steps and implement multiple image upload in laravel 7.x, 6.x, 5 app:

  1. Install Laravel App
  2. Setup Database
  3. Generate Migration & Model
  4. Add Route
  5. Create Controller & Methods
  6. Create Blade View
  7. Make Folder
  8. Start Development Server

Step 1: Install Laravel App

First of all, you need to download laravel fresh setup. Use the below command and download fresh new laravel setup :

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

Step 2: Setup Database

After successfully install laravel Application, Go to your project .env file and set up database credential and move next step :

 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

Now, you will create table name Image and it’s migration file. use the below command :

php artisan make:model Image -m

It command will create one model name Image and also create one migration file for Image table. After successfully run the command go to database/migrations file and put the below here :

<?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->increments('image');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('images');
    }
}

Next, migrate the table using the below command :

php artisan migrate

Step 4: Add Route

In this step, you need to add two routes in web.php file. Navigate to app/routes folder and open web.php file and create two below routes here :

 Route::get('image', 'ImageController@index');
 Route::post('save', 'ImageController@save');
<div class="gen-info-box">
<h4>Recommended Post</h4>
<a href="https://www.tutsmake.com/laravel-6-upload-image-using-dropzone-js-tutorial/
">Laravel Upload image Using Dropzone Example</a>
</div>

Step 5: Create Controller & Methods

Now, you need to create a controller name ImageController. So run the following command and create ImageController:

php artisan make:controller ImageController

After successfully create controller. Navigate to app/http/controllers folder and ImageController.php. Then add the following code into your ImageController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator,Redirect,Response,File;
use App\Image;

class ImageController extends Controller
{

    public function index()
    {
        return view('image');
    }

    public function save(Request $request)
    {

        request()->validate([

            'image' => 'required',
            'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'

        ]);

        if ($image = $request->file('image')) {
            foreach ($image as $files) {
            $destinationPath = 'public/image/'; // upload path
            $profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
            $files->move($destinationPath, $profileImage);
            $insert[]['image'] = "$profileImage";
            }
        }
      
        $check = Image::insert($insert);

        return Redirect::to("image")->withSuccess('Great! Image has been successfully uploaded.');

    }
}

Step7: Create Blade view

In this step, Create blade view file. Go to /resources/views and create one file name image.blade.php :

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Laravel 5.7 Multiple Image Upload Example - Tutsmake.com</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />

 <style>
    .container{
    padding: 10%;
    text-align: center;
   } 

 </style>
</head>
<body>

<div class="container">
    <h2 style="margin-left: -48px;">Laravel 5.7 Multiple Image Upload Example - Tutsmake.com</h2>
    <br>

    <br>
    @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>
    <br>
    @endif

    @if (count($errors) > 0)

        <div class="alert alert-danger">

            <strong>Opps!</strong> There were something went wrong with your input.

            <ul>

                @foreach ($errors->all() as $error)

                    <li>{{ $error }}</li>

                @endforeach

            </ul>

        </div>
      <br>
    @endif
    <form action="{{ url('save') }}" method="post" accept-charset="utf-8" enctype="multipart/form-data">
      @csrf
    <div class="avatar-upload col-6">
       
        <div class=" form-group avatar-edit">
            <input type='file' id="image" name="image[]" accept=".png, .jpg, .jpeg" multiple="">
            <label for="imageUpload"></label>
            
        </div>

    </div>
    <div class="form-group col-3">
    <button type="submit" class="btn btn-success">Submit</button>
    </div>
    </form>
</div>

</body>
</html>

Step 7: Start Development Server

Run the following command to start the development server. So use the php artisan serve command and start your server:

 php artisan serve

 If you want to run the project diffrent port so use this below command 

 php artisan serve --port=8080  

Now you are ready to run our example so run bellow command to quick run.

 http://localhost:8000/image
Or direct hit in your browser
http://localhost/ImageUpload/public/image

Conclusion

In this tutorial , you have successfully uploaded multiple images in project folder. And as well as database and folder. Our examples run quickly.

Recommended Laravel Posts

  1. Laravel Upload Image Using Dropzone js Tutorial
  2. Upload Files/Images to Amazon s3 Cloud Using Laravel Filesystem
  3. Laravel CKEditor with Image Upload
  4. Ajax Image Upload in Laravel Example From Scratch
  5. Laravel Intervention Upload Image Using Ajax – Example
  6. Laravel Upload Image to Database with Validation
Lara Multiple Image upload example look like this :

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

One reply to Multiple Image Upload In Laravel App

  1. Thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *