Laravel 8 Multiple Image Upload Example

10-Apr-2023

.

Admin

Laravel 8 Multiple Image Upload Example

Hi Guys,

In this tutorial, I am going to learn you laravel 8 multiple image upload example. We will implement a multiple image upload example for beginners in laravel 8 application. i will give you simple example of how to upload multiple image in laravel 8. you will learn upload multiple image in laravel 8.

Laravel 8 is just released at few days ago, Laravel 8 gives several new features and LTS support. So if you are new to laravel then this tutorial will help you create insert update delete application in laravel 8.

Here I will give you full example for how to upload multiple image in laravel 8. So, let's follow few step to create example of laravel 8 multiple image upload tutorial.

Step 1 : Install Laravel 8


In the first step, we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

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

Step 2 : Database Configuration

In second step, we will make database Configuration for example database name, username, password etc for our multiple image application of laravel 8 So lets open .env file and fill all deatils like as bellow:

.env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Step 2 : Database Configuration

we are going to create multiple image upload application for images. so we have to create migration for "image" table using Laravel 8 php artisan command, so first fire bellow command:

php artisan make:migration create_images_table --create=images

After this command you will find one file in following path "database/migrations" and you have to put bellow code in your migration file for create images table

<?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->id();

$table->string('images');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('images');

}

}

Now you have to run this migration by following command:

php artisan migrate

Step 4: Add Resource Route

Here, we need to add route for multiple image upload form. so open your "routes/web.php" file and add following route.

routes/web.php

use App\Http\Controllers\ImageController;

Route::get('multiple-image-upload', [ImageController::class, 'multipleImage'])->name('multiple.image');

Route::post('multiple-image/store', [ImageController::class, 'multipleImageStore'])->name('multiple.image.store');

Step 5: Add Controller and Model

In this step, now we should create new controller as ImageController. So run bellow command and create new controller. bellow controller for create ImageControler.

php artisan make:controller ImageController --model=Image

After bellow command you will find new file in this path "app/Http/Controllers/ImageController.php".

In this controller will create two methods as bellow:

1)multipleImage()

2)multipleImageStore()

So, let's copy bellow code and put on ImageController.php file.

app/Http/Controllers/ImageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Image;

class ImageController extends Controller

{

/**

* Display a listing of the myformPost.

*

* @return \Illuminate\Http\Response

*/

public function multipleImage()

{

return view('multipleImage.imageUpload');

}

/**

* Display a listing of the myformPost.

*

* @return \Illuminate\Http\Response

*/

public function multipleImageStore(Request $request)

{

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

{

$imageName=$image->getClientOriginalName();

$image->move(public_path().'/images/', $imageName);

$fileNames[] = $imageName;

}

$images = json_encode($fileNames);

// Store $images image in DATABASE from HERE

Image::create(['images' => $images]);

return back()

->with('success','You have successfully file uplaod.')

->with('files',$fileNames);

}

}

Ok, so after run bellow command you will find "app/Models/Image.php" and put bellow content in Image.php file:

app/Models/Image.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class Image extends Model

{

use HasFactory;

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'images',

];

}

Step 6: Add Blade Files

In last step. In this step we have to create just blade files. So mainly we have to create imageUpload file and then create new folder "multipleImage" then create blade file of multiple image upload. So finally you have to create one following bellow blade file:

1) imageUpload.blade.php

So let's just create following file and put bellow code.

resources/views/multipleImage/imageUpload.blade.php

resources/views/multipleImage/imageUpload.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 8 Multiple Image Upload example - NiceSnippets.com</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<style type="text/css">

h2{

text-align: center;

font-size:22px;

margin-bottom:50px;

}

body{

background:#f2f2f2;

}

.section{

margin-top:150px;

padding:50px;

background:#fff;

}

</style>

<body>

<div class="container">

<div class="col-md-8 section offset-md-2">

<div class="panel panel-primary">

<div class="panel-heading">

<h2>Laravel 8 Multiple Image Upload example - NiceSnippets.com</h2>

</div>

<div class="panel-body">

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

@endif

@if (count($errors) > 0)

<div class="alert alert-danger">

<strong>Whoops!</strong> There were some problems with your input.

<ul>

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

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

@endforeach

</ul>

</div>

@endif

<form action="{{ route('multiple.image.store') }}" method="POST" enctype="multipart/form-data">

@csrf

<div class="row">

<div class="col-md-10">

<input type="file" name="file[]" accept="image/*" multiple="multiple" class="form-control">

</div>

<div class="col-md-2">

<button type="submit" class="btn btn-success">Upload</button>

</div>

</div>

<div class="row mt-3">

@if ($images = Session::get('files'))

@foreach($images as $value)

<div class="col-md-2">

<img src="{{ asset('images/'.$value) }}" width="100">

</div>

@endforeach

@endif

</div>

</form>

</div>

</div>

</div>

</div>

</body>

</html>

Now we are ready to run multiple image upload application example with laravel 8 so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

localhost:8000/multiple-image-upload

You will see layout as like bellow:

Show Form

Upload Image

I hope it can help you....

#Laravel 8

#Laravel