Throughout this Laravel 9 upload multiple images with preview example tutorial you will learn how to upload multiple images in laravel 9 app with show preview.
Before uploading multiple images into the database and folder in laravel 9 app, will display the preview of images using jQuery. Also, validate image mime type, size, height, width on the controller method.
Laravel upload multiple files with preview will works in laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps;
Multiple Image Upload With Preview In Laravel 9
Follow the following steps to upload multiple images with preview and validation in laravel 9 app:
- Step 1: Download Laravel 9 Application
- Step 2: Connect Database with App
- Step 3: Build Image Model & Migration
- Step 4: Create Routes
- Step 5: Generate Controller
- Step 6: Create Blade View
- Step 7: Start Development Server
Install Laravel Application
First of all, Open terminal OR command prompt and run following command to install laravel fresh app:
composer create-project laravel/laravel multiple-images-preview
Now Go into the app:
cd multiple-images-preview
Connect Database to App
Open the .env file and add your database credentials such as database name, username equally important password:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=multiple_images DB_USERNAME=root DB_PASSWORD=
Create Model & Run Migration
Next step, we need to execute the following command on terminal to generate model, migration file using the following command:
php artisan make:model Image -m
Next, add fillable properties in you model. So, open the app\Models\Image.php file and put the following code on it;
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
use HasFactory;
protected $fillable = [
'name', 'path'
];
}
Next, open the database\migrations\2020_09_13_082159_create_images_table.php file and add the columns in it.
<?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('name');
$table->string('path');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('images');
}
}
Now run the below command and generate table in your database.
php artisan migrate
Make Routes
In this step, Navigate to routes folder and open web.php. Then add the following routes into web.php file:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
Route::get('images', [ ImageController::class, 'index' ]);
Route::post('images', [ ImageController::class, 'store' ])->name('images.store');
Create Controller & Update Code
Go to console run the artisan command to create the image controller:
php artisan make:controller ImageController
After successfully create controller open and put the below code on it, Here we have added two methods on it one index for showing for form and store for saving data in database and uploading in public and storage directory.
So, open the app\Http\Controllers\ImageController.php file and put the login inside it;
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Image;
class ImageController extends Controller
{
public function index()
{
return view('images');
}
public function store(Request $request)
{
$request->validate([
'images' => 'required',
'images.*' => 'mimes:jpg,png,jpeg,gif,svg'
]);
if ($request->hasfile('images')) {
$images = $request->file('images');
foreach($images as $image) {
$name = $image->getClientOriginalName();
$path = $image->storeAs('uploads', $name, 'public');
Image::create([
'name' => $name,
'path' => '/storage/'.$path
]);
}
}
return back()->with('success', 'Images uploaded successfully');
}
}
Create Blade File
Now create a images.blade.php file inside the resources/views directory. After that open the resources\views\images.blade.php file and put the below code on it;
<!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">
<title>Upload Multiple Files in Laravel with preview</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<style>
.invalid-feedback {
display: block;
}
.images-preview-div img
{
padding: 10px;
max-width: 150px;
}
</style>
</head>
<body>
<div class="container mt-4">
<h2>Upload Multiple Files in Laravel</h2>
@if(session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
@endif
<form method="post" action="{{ route('images.store') }}" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" name="images[]" multiple class="form-control" accept="image/*" id="images">
@if ($errors->has('images'))
@foreach ($errors->get('images') as $error)
<span class="invalid-feedback" role="alert">
<strong>{{ $error }}</strong>
</span>
@endforeach
@endif
</div>
<div class="col-md-12">
<div class="images-preview-div"> </div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Save</button>
</div>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script >
$(function() {
// Multiple images preview with JavaScript
var previewImages = function(input, imgPreviewPlaceholder) {
if (input.files) {
var filesAmount = input.files.length;
for (i = 0; i < filesAmount; i++) {
var reader = new FileReader();
reader.onload = function(event) {
$($.parseHTML('<img>')).attr('src', event.target.result).appendTo(imgPreviewPlaceholder);
}
reader.readAsDataURL(input.files[i]);
}
}
};
$('#images').on('change', function() {
previewImages(this, 'div.images-preview-div');
});
});
</script>
</body>
</html>
Start Laravel App
After all instruction its time to test laravel 9 multiple image upload with preview…
Next, run the following command on cmd to start the development server:
php artisan serve
Next open your browser and fire the following URL into a browser:
http://127.0.0.1:8000/images
Hope this laravel multiple images upload in laravel with preview example help you.