How to Multiple Image Upload in Laravel 10 API

Websolutionstuff | Dec-01-2023 | Categories : Laravel

Hello everyone! I'm excited to share with you how I'm enhancing my Laravel 10 API by enabling the capability to handle multiple image uploads. This step-by-step guide will walk us through the process, making it simple and straightforward.

By the end, my API will be able to effortlessly manage and process multiple images, adding a powerful feature to my application.

So, let's jump in together and explore the simplicity of implementing multiple image uploads in Laravel 8, Laravel 9, and Laravel 10 POSTMAN API.

image_upload_api

So, let's see laravel 10 multiple image uploads using POSTMAN API.

Here's a step-by-step guide on how to implement multiple image uploads in a Laravel 10 API:

Step 1: Create a New Laravel 10 Project

If you haven't already, create a new Laravel 10 project using the following command:

composer create-project laravel/laravel image-upload-api-laravel-10

 

Step 2: Set Up Your Database

Configure your database settings in the .env file and run migrations to create necessary tables.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password

 

Step 3: Create a Model & Migration

Create a model and migration using the following command.

php artisan make:model Image -m

database/migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateImagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('path');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('images');
    }
}

Now, run migrations using the following command.

php artisan migrate

 

Step 4: Create API Routes

Define API routes for handling image uploads in routes/api.php:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\API\ImageUploadController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::post('multiple-image-upload', [ImageUploadController::class, 'upload']);

 

Step 5: Create ImageUploadController

Generate a controller to handle image uploads:

php artisan make:controller API\ImageUploadController

app/http/controllers/API/ImageUploadController.php

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Image;
use Validator;

class ImageUploadController extends Controller
{
	public function store(Request $request)
	{
		if(!$request->hasFile('fileName')) {
			return response()->json(['upload_file_not_found'], 400);
		}

		$allowedfileExtension=['pdf','jpg','png'];
		$files = $request->file('fileName'); 
		$errors = [];

		foreach ($files as $file) {      
			$extension = $file->getClientOriginalExtension();
			$check = in_array($extension,$allowedfileExtension);

			if($check) {
				foreach($request->fileName as $mediaFiles) {
					$name = $mediaFiles->getClientOriginalName();
					$path = $mediaFiles->store('public/images');
							
					$save = new Image();
					$save->title = $name;
					$save->path = $path;
					$save->save();
				}
			} else {
				return response()->json(['invalid_file_format'], 422);
			}

			return response()->json(['file_uploaded'], 200);
		}
	}
}

 

Step 6: Test with Postman

Run your Laravel development server:

php artisan serve

 

Conclusion:

That's it! You've successfully implemented multiple image uploads in your Laravel 10 API.

 


You might also like:

Recommended Post
Featured Post
How to Install Node JS and NPM on Ubuntu 22.04
How to Install Node JS and NPM...

Hey fellow Ubuntu enthusiasts! 🐧✨ If you're looking to step into the awesome world of Node.js and npm for your web d...

Read More

Jan-12-2024

Laravel 9 Inner Join Query Example
Laravel 9 Inner Join Query Exa...

In this article, we will see laravel 9 inner join query example. Also, see how to join two tables in laravel 9. In larav...

Read More

Mar-30-2022

Crop Image Before Upload Using Croppie Plugin
Crop Image Before Upload Using...

In this article, we will see how to crop images before uploading using the croppie plugin. any times we have requir...

Read More

Aug-15-2020

How To Setup 404 Page In Angular 12
How To Setup 404 Page In Angul...

In this article, we will see how to set up a 404 page in angular 12.  To set up a 404 page in the angular...

Read More

May-11-2022