Upload Multiple Image In Laravel 8.x Using JQuery

Hello Artisan in this example i will discuss from scratch about multiple image upload in laravel 8 version. Laravel 8 multiple image upload example is the todays topic. I will use jquery to take input multiple. Here in this multiple imgae upload you will learn laravel 8 multiple images upload. We will look at this example of multiple image upload laravel 8 step by step.

If you don't know laravel upload multiple images method then this tutorial is for you. Sometimes we need to upload multiple image in our web application. In this example i will help you laravel 8 multiple image upload with preview. Alright, let’s dive into the steps one after another.

In this multiple image upload tutorial i will create simple multiple image upload in laravel 8. So you can use this code on your laravel 8 application. You can see the preview from below image.

 

Step 1: Download Laravel 8

In the first step, i will download a new simple copy source code of Laravel App project by typing the some following command.

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

 

Step 2 : Create Migrations

Here, we need create database migration for files table and also we will create model for files table.

php artisan make:mode Image

 

database/migration/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('image');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('images');
    }
}

 

Step 3: Create Routes

In this step, i will create routes for multiple file upload. so create two route with GET and POST route example.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\FileController;
  
Route::get('image', [FileController::class, 'create']); 
Route::post('image', [FileController::class, 'store']);

 

Step 4: Create Controller

Now we require to add new FileController controller for manage route So let's create FileController with create and store method. Make sure you need to create "files" folder in your public directory.

app/Http/Controllers/FileController.php

  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\File;
  
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, [
                'image' => 'required',
        ]);
  
      if ($files = $request->file('image')) {
      
            $destinationPath = public_path('/product_image/');

            foreach($files as $img) {
         
              $image = $img->getClientOriginalName();
              $img->move($destinationPath, $image);
         
              $image = new Image();
              $image->image = $image;
              $image->save();

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

 

Step 5: Create Blade File

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

resources/views/create.blade.php

 

Read also : Laravel Bootstrap Tabs with Dynamic Content Loading

 

Now all are set to. Now you can check it that how to upload multiple image in laravel. 

 

#laravel #laravel-8x #laravel-8 #multiple #image-upload #file-upload