Laravel 8 multiple image file upload via API using postman example; In this tutorial, we would like to share with you how to upload multiple image file via api in laravel 8.
If you work with laravel apis and want to upload multiple image file using postman or ajax. And also want to validate files or images before uploading to server via API or ajax in laravel 8.
So, in this tutorial, you will learn how to upload multiple image file via API using postman in laravel 8 app.
Laravel 8 Multiple Image File Upload Via Api Tutorial
Use the following steps to upload multiple image file via API using postman in laravel 8 applications:
- Step 1: Install Laravel 8 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 8 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 8 APIs using postman app. So start postman app and use the following URL to upload multiple image files via api in laravel 8 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 8 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.