Multiple Image Upload In Laravel 8
In this article, we will learn about multiple image upload in laravel 8.upload multiple image in laravel 8 is very easy.I will guide you step by to upload multiple image in laravel 8.we will create migration, controller, model in order to create multiple image upload in laravel 8.I will also show you how we can add validation in multiple image upload in laravel 8. Please follow the below steps in order to create multiple image upload in Laravel 8.
We will follow below steps to create multiple image upload in laravel 8.
- Download Laravel 8
- Create migration and model
- create routes
- create controller
- create blade
STEP – 1 Install Laravel project to begin your journey with multiple image uploads in Laravel 8.
composer create-project --prefer-dist laravel/laravel multiple-image
STEP – 2 Create model and migration
In this step will create model and migration to do our multiple image upload in laravel 8. run the below command to create model and migration together.
php artisan make:model File -m
now go to your app/Http/Models/File.php and write the below code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
use HasFactory;
protected $guarded = [""];
}
Now go to app/database/migrations/ and look for 2020_12_09_104645_create_files_table.php and add the below code.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFilesTable extends Migration
{
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('filenames');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('files');
}
}
now run php artisan migrate it will create a table in your database which we will use to upload multiple image in laravel 8
STEP – 3 Create routes
Now we are going to create routes.
Before adding a route in web.php we have to add our controller namespace on the top.
use App\Http\Controllers\FileController;
Now add the below routes in your web.php file
Route::get('/file', [FileController::class, 'create']);
Route::post('/file', [FileController::class, 'store']);
STEP – 4 Create a controller
Now we are going to create controller to do multiple image upload in laravel 8. To create the controller run the below command.
php artisan make:controller FileController
Now go to app/Http/Controller/FileController.php and add the below code
<?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->validate($request, [
'filenames' => 'required',
'filenames.*' => 'mimes:doc,pdf,docx,zip'
]);
if($request->hasfile('filenames'))
{
foreach($request->file('filenames') as $file)
{
$name = time().'.'.$file->extension();
$file->move(public_path().'/files/', $name);
$data[] = $name;
}
}
$file= new File();
$file->filenames=json_encode($data);
$file->save();
return back()->with('success', 'Data Your files has been successfully added');
}
}
STEP – 5 Create a blade file
Now we will write code in our blade file.
<html lang="en">
<head>
<title>Laravel 8 Multiple File Upload Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</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 8 Multiple File Upload</h3>
<form method="post" action="{{url('file')}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control" multiple>
<div class="input-group-btn">
<button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
</div>
</body>
</html>
I hope you understand how we can do multiple image uploads in Laravel.