Laravel 9 Image File Upload via API using Postman

Laravel 9 Image File Upload via API using Postman

Multiple file upload in laravel 9 api using postman; In this tutorial, we will show you how to upload multiple image files via API in laravel 9.

Laravel 9 Image File Upload via API using Postman

Use the following steps to upload multiple image files via API using postman in laravel applications:

  • Step 1: Install Laravel 9 App
  • Step 2: Database Configuration with App
  • Step 3: Make Migration & Model
  • Step 4: Make Api Routes
  • Step 5: Generate API Controller by Artisan
  • Step 6: Run Development Server
  • Step 7: Laravel Multiple Image File Upload Via Api Using PostMan

Step 1: Install Laravel 9 App

First of all, open your terminal and run the following command to install or download laravel fresh application setup for uploading files via laravel api:

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

Step 2: Add Database Credentials

Next, Navigate to your downloaded laravel app root directory and open .env file. Then add your database details in .env file, as follow:

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Step 3: Make Migration & Model

In this step, open a command prompt and run the following command:

php artisan make:model Image -m

This command will create one model name file and as well as one migration file for the Images table.

Then Navigate to database/migrations folder and open create_images_table.php. Then update the following code into create_images_table.php:

<?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');
    }
}

After that, run the following command to migrate the table into your select database:

php artisan migrate

Step 4: Make API Route

In this step, Navigate to the app/routes folder and open api.php file. Then update the following routes into your api.php file:

use App\Http\Controllers\API\MultipleUploadController;

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

Step 5: Generate API Controller by Artisan

In this step, open your terminal and run the following command:

php artisan make:controller API\MultipleUploadController

Note that, This command will create a controller named MultipleUploadController.php file.

Now app/http/controllers/API folder and open MultipleUploadController.php. Then update the following file uploading methods into your MultipleUploadController.php file:

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;

use App\Models\Image;

use Validator;

use Illuminate\Http\Request;

class MultipleUploadController 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) {

                $path = $mediaFiles->store('public/images');
                $name = $mediaFiles->getClientOriginalName();
     
                //store image file into directory and db
                $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: Run Development Server

In this step, run the following command to start the development server:

 php artisan serve

Step 7: Laravel Multiple Image File Upload Via Api Using PostMan

Finally, uploading multiple image files via laravel 9 APIs using postman app. So start postman app and use the following URL to upload multiple image files via api in laravel app:

http://127.0.0.1:8000/multiple-image-upload

Conclusion

In this tutorial, you have learned how to upload multiple image file in laravel apps via api using postman.

Recommended Laravel Posts

Recommended:-Laravel Try Catch

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *