Laravel 10 Image File Upload Rest Api Postman Example

Laravel 10 Image File Upload Rest Api Postman Example

If you create Rest API to upload file using the Laravel web application and want to upload images and files using Postman through this Rest API. So, in this tutorial, you will learn how to upload files/images via rest APIs using postman in Laravel 10 apps.

Laravel 10 File Image Upload Rest Api Postman Example

Steps to upload single or multiple image file via Rest API using Postman in Laravel 10 applications:

  • Step 1: Setup New Laravel 10 App
  • Step 2: Configure Database with Laravel App
  • Step 3: Make Migration & Model
  • Step 4: Add Api Routes
  • Step 5: Generate API Controller by Artisan
  • Step 6: Run Development Server
  • Step 7: Test single or multiple image file upload in laravel via api using postman

Step 1: Setup New Laravel 10 App

Firstly, start your terminal and execute the following command to install or download laravel 10 application for uploading images/files via laravel api with postman:

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

Step 2: Configure Database with Laravel App

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 again your terminal/command line/ command prompt. And execute the following command into it to create model and migration files for your laravel applications:

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: Add Api Routes

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: Test single or multiple image file upload in laravel via api using postman

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

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

Conclusion

In this tutorial, you have learned how to upload single or multiple image files in Laravel 10 apps via api using Postman.

Recommended Laravel Posts

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 *