Hello developers!, In this article, we'll see how to upload multiple images in laravel 10 API. Here, we'll upload multiple images using REST API in laravel 10. Here, we'll create API for multiple image uploads in laravel 10 with the help of POSTMAN.
In this guide, we'll create an Image model, migration, and API function to store the image.
Laravel 10 multiple image uploads using POSTMAN API
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
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
Create a model and migration using the following command.
php artisan make:model Image -m
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
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']);
Now, create a controller using the following command.
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=['jpg','png','jpeg'];
$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);
}
}
}
Now, run laravel 10 application using the following command.
php artisan serve
You might also like:
- Read Also: How to Create a Newsletter Step by Step in Laravel 10
- Read Also: How to Create Bar Chart in Laravel 10 using Apexcharts
- Read Also: How to Delete Relationship Records in Laravel 10
- Read Also: Laravel 10 REST API CRUD Operation