In this tutorial you will learn about the Laravel 8 Multiple Image Upload Via API and its application with practical example.
In this Laravel 8 multiple image file upload via API tutorial I will show you how to upload multiple image file via api in laravel 8. In this tutorial we will learn to upload multiple images using api in laravel 8. We will also learn to create api to upload images in laravel 8.
While working with laravel application we come to situation when we want to upload multiple image file using rest api. We may also want to validate files or images before uploading to server via API or ajax in laravel 8. In this article we will learn to create rest api to upload multiple image in laravel 8.
Laravel 8 Multiple Image Upload Via API
In this step by step tutorial I will show you how to upload multiple image file via API using postman in laravel 8 application.
- 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 we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2: Add Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
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
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Image -m |
The above command will create a model name file and a migration file for the Images table. Now, go to database/migrations folder and open create_images_table.php. Then put the following code into create_images_table.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?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, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 4: Make API Route
After this, we need to define routes in “routes/api.php” file. Lets open “routes/api.php” file and add the following routes in it.
routes/api.php
1 2 3 |
use App\Http\Controllers\API\MultipleUploadController; Route::post('multiple-image-upload', [MultipleUploadController::class, 'upload']); |
Step 5: Generate API Controller by Artisan
Now, lets create a controller named MultipleUploadController using command given below –
1 |
php artisan make:controller API\MultipleUploadController |
The above 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?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
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Step 7: Laravel Multiple Image File Upload Via Api Using PostMan
Now start postman and use the following URL to upload multiple image files via api in laravel 8 app:
1 |
http://127.0.0.1:8000/multiple-image-upload |