In this tutorial you will learn about the Laravel 7 File Upload Via API Example From Scratch and its application with practical example.
In this Laravel 7 File Upload Via API Example From Scratch tutorial I’ll show you how to create file upload api in laravel. In this tutorial you will learn to create API to upload file in laravel project.
Laravel 7 File Upload Via API Example From Scratch
- Step 1: Install Laravel New App
- Step 2: Add Database Credentials
- Step 3: Generate Migration & Model
- Step 4: Create Routes For File
- Step 5: Generate Controller by Artisan
- Step 6: Run Development Server
- Step 7: Laravel Upload File Via Api Using PostMan
Step 1: Install Laravel New App
First of all we need to create a fresh laravel project, download and install Laravel 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: Generate Migration & Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Documents -m |
This command will create one model name file and as well as one migration file for the Documents table. Then Navigate to database/migrations folder and open create_documents_table.php. Then update the following code into create_documents_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 CreatedocumentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('documents', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->integer('user_id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('documents'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 4: Create Route For File
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 |
Route::prefix('v1')->group(function(){ Route::post('store-file', 'DocumentController@store'); }); |
Step 5: Generate Controller by Artisan
Now, lets create a controller named DocumentController using command given below –
1 |
php artisan make:controller Api\DocumentController |
Note that, This command will create a controller named DocumentController.php file.
Now app/controllers/Api folder and open DocumentController.php. Then update the following file uploading methods into your DocumentController.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 |
<?php namespace App\Http\Controllers\Api; use Illuminate\Http\Request; use Validator,Redirect,Response,File; use App\Document; use Validator; class DocumentController extends Controller { public function store(Request $request) { $validator = Validator::make($request->all(), [ 'user_id' => 'required', 'file' => 'required|mimes:doc,docx,pdf,txt|max:2048', ]); if ($validator->fails()) { return response()->json(['error'=>$validator->errors()], 401); } if ($files = $request->file('file')) { //store file into document folder $file = $request->file->store('public/documents'); //store your file into database $document = new Document(); $document->title = $file; $document->user_id = $request->user_id; $document->save(); return response()->json([ "success" => true, "message" => "File successfully uploaded", "file" => $file ]); } } } |
If you want to upload images via api instead of files in laravel. So you can change in validation rules on the controller file, as follow:
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 Upload File Via Api Using PostMan
1 |
http://localhost:8000/store-file |