Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Today, I will give you an example of “How to Store multiple records using post API in Laravel”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, laravel 8, and Laravel 9 application.
First, what we’re doing here, This is the example :
We use Post API to store multiple records in our table in Laravel. In this example, We will create Post API to store an array or JSON with multiple records using the Postman API Platform, We will return an array of books data to the database with Post API with JSON request via Postman.
Let’s get started.
Generating Migration with Model
php artisan make:model Book -m
Migration Structures
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('book_name');
$table->string('book_author');
$table->string('book_description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
Run Migration
php artisan migrate
app\Models\Task.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
use HasFactory;
}
Create a Controller
php artisan make:controller Api/BooksController
routes\api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\BooksController;
/*
|--------------------------------------------------------------------------
| 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::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
#Api Post Array(store multiple records)
Route::post('/add/multiple/books',[BooksController::class, 'addMultipleBooks']);
app\Http\Controllers\Api\BooksController.php
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Book;
class BooksController extends Controller
{
public function addMultipleBooks(Request $request){
if($request->isMethod('post')){
$bookData = $request->all();
foreach($bookData['books'] as $key => $value){
$book = new Book;
$book->book_name = $value['book_name'];
$book->book_author = $value['book_author'];
$book->book_description = $value['book_description'];
$book->save();
}
return response()->json(['message'=>'Books added Successfully']);
}
}
}
Setup and Test Postman for Post API
We test our Post API in Postman, please copy your API URL Select the Post option in the dropdown, click on headers and fill in this information because we are sending an array or JSON:-
Key: Content-type Value: application/json
Create an array to store multiple records in the books table.
To verify your JSON data visit:- https://jsonlint.com.
foreach($bookData['books'] as $key => $value)]
{
...
...
...
}
Note-: We have passing books object in post API array, so please also pass this object in your controller.
Now hit the send button to store your array:-
Read also-: How to validate API in Laravel.