Hello Artisan,
In this laravel 9 multiple image upload, i am going to show you a demo application of multiple image upload tutorial in Laravel 9. I will talk about laravel 9 multiple image upload example with an explanation step by step.
So I hope this example will help you to learn laravel 9 multiple images upload. it's going to be a simple example of multiple image upload laravel 9. Here you will learn laravel 9 multiple image upload with a preview from scratch.
Here, we will install a fresh laravel 9 and create a simple form with a file input field that you can use to select multiple images to store the database. Let's see the example code of Laravel 9 multiple image upload.
Step 1: Download Laravel 9
We are going to start it from scratch. So download a fresh Laravel application to create this laravel 9 multiple image upload.
composer create-project laravel/laravel example-app
Step 2: Create Migration and Model
Here, we will create migration for images
table, let's run the below command and update the code.
php artisan make:migration create_images_table
And update like:
database/migrations/2022_02_24_140040_create_images_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('images');
}
};
Now after setting up your database configuration run the below command:
php artisan migrate
Now we will create an Image
model by using the following command:
php artisan make:model Image
Now update this newly created model like:
app/Models/Image.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
use HasFactory;
protected $fillable = [
'name'
];
}
Read also: Laravel 9 Image Upload Tutorial Step By Step
Step 3: Create Controller
In this third step, I will create a new ImageController
; in this file, I will add two methods index()
and store()
for render view and store images into folder and database logic.
app/Http/Controllers/ImageController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Image;
class ImageController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('imageUpload');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'images' => 'required',
'images.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$images = [];
if ($request->images){
foreach($request->images as $key => $image)
{
$imageName = time().rand(1,99).'.'.$image->extension();
//for storage upload
/* $image->storeAs('images', $imageName); */
$image->move(public_path('images'), $imageName);
$images[]['name'] = $imageName;
}
}
foreach ($images as $key => $image) {
Image::create($image);
}
return back()
->with('success','You have successfully upload image.')
->with('images', $images);
}
}
Step 4: Add Routes
Now, open routes/web.php
file and add the routes to manage to GET and POST requests for render view and store image logic.
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::controller(ImageController::class)->group(function(){
Route::get('image-upload', 'index');
Route::post('image-upload', 'store')->name('image.store');
});
Step 5: Create Blade View
At the last step, we need to create a imageUpload.blade.php
file and in this file, we will create a form with a file input button. So copy bellow and put it on that file.
resources/views/imageUpload.blade.php
Read also: Create Your First Laravel 9 CRUD Application Step By Step
Hope this Laravel 9 multiple image upload tutorial will help you.
#laravel #laravel-9x