Multiple Image Upload in Laravel 6 Tutorial

10-Apr-2023

.

Admin

Multiple Image Upload in Laravel 6 Tutorial

Hi guys,

In this blog, I will show how to Multiple Image Upload in laravel 6. we will explain multiple image upload in laravel 6.we will use laravel image validation and store to folder and database for laravel 6 multiple image upload. I will create simple multiple image upload in laravel 6.

we will upload multiple file and store on server then after we will store database too. so in this example we will create "images" table using laravel migration and write code for route, controller and view step by step.

Here the example of multiple image upload in laravel 6

Step 1 : Install Laravel 6 Application


we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Database Configuration

In this step, we require to make database configuration, you have to add following details on your .env file.

1.Database Username

1.Database Password

1.Database Name

In .env file also available host and port details, you can configure all details as in your system, So you can put like as bellow:

following path: .env

DB_HOST=localhost

DB_DATABASE=homestead

DB_USERNAME=homestead

DB_PASSWORD=secret

Step 2: Create images Table and Model

In this step we have to create migration for images table using Laravel 6 php artisan command, so first fire bellow command:

php artisan make:model Image -m

After this command you have to put bellow code in your migration file for create images table.

following path: /database/migrations/2020_01_10_102325_create_images_table.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateimagesTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('images', function (Blueprint $table) {

$table->bigIncrements('id');

$table->string('imagename');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('images');

}

}

Now we require to run migration be bellow command:

php artisan migrate

After you have to put bellow code in your model file for create images table.

following path:/app/Image.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Image extends Model

{

/**

* Run the migrations.

*

* @return void

*/

protected $fillable = [

'imagename',

];

}

Step 3: Create Route

In this is step we need to create route for Multiple Image Upload layout file

following path:/routes/web.php

Route::get('multiple-image','MultipleImageController@create');

Route::post('multiple-image','MultipleImageController@store');

Step 4: Create Controller

here this step now we should create new controller as MultipleImageController,So run bellow command for generate new controller

php artisan make:controller MultipleImageController

now this step, this controller will manage Multiple Image Upload layout bellow content in controller file.following fille path

following path:/app/Http/Controllers/MultipleImageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Image;

class MultipleImageController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('multipleImage');

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$this->validate($request, [

'imagename' => 'required',

'imagename.*' => 'mimes:jpg,png,jpeg,gif'

]);

if($request->hasfile('imagename'))

{

foreach($request->file('imagename') as $file)

{

$name = time().'.'.$file->extension();

$file->move(public_path().'/images/', $name);

$data[] = $name;

}

}

$file= new Image();

$file->imagename=json_encode($data);

$file->save();

return back()->with('success', 'Your Image has been successfully added');

}

}

After you need to create "images" folder in your public directory.

Step 5: Create Blade File

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

following path:/resources/views/multipleImage.blade.php

<html lang="en">

<head>

<title>Multiple Image Upload in Laravel 6 Tutorial - nicesnippets.com</title>

<script src="jquery/1.9.1/jquery.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css">

</head>

<body>

<div class="container lst">

@if (count($errors) > 0)

<div class="alert alert-danger">

<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">Multiple Image Upload in Laravel 6 Tutorial - nicesnippets.com</h3>

<form method="post" action="{{url('multiple-image')}}" enctype="multipart/form-data">

{{csrf_field()}}

<div class="input-group hdtuto control-group lst increment" >

<input type="file" name="imagename[]" class="myfrm form-control">

<div class="input-group-btn">

<button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>

</div>

</div>

<div class="clone hide">

<div class="hdtuto control-group lst input-group" style="margin-top:10px">

<input type="file" name="imagename[]" class="myfrm form-control" multiple="

multiple">

<div class="input-group-btn">

<button class="btn btn-danger" type="button"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>

</div>

</div>

</div>

<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>

</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 we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/multiple-image

It will help you...

#Laravel

#Laravel 6