Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

basirafeef's avatar

How to insert Json array of Json objects using laravel database system?

i have an api which accept multiple json objects as json array for insertion, how to convert json array to php array that laravel database systems accept for multiple insertion or direct insert json using laravel database systems? please help me with some examples.

json array of json objects : $jsonData = [ {"name" : "name 1", "age" : 18}, {"name" : "name 2", "age" : 19}, {"name" : "name 3", "age" : 18} ]; To php array : $phpArryOfJsonData = [ ["name"=>"name 1", "age"=> 18], ["name"=>"name 2", "age"=> 19], ["name"=>"name 3", "age"=> 18] ];

database mode or DB: User :: insert($phpArryOfJsonData);

Thanks a lot for helping me.

0 likes
20 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102
User::insert(ison_decode($jsonData, true));
1 like
basirafeef's avatar

@Sinnbeck $bodydata = $request->getParsedBody();

    if (!empty($bodydata)) {
        $result = User :: insert(json_decode($bodydata, true));
	}

Warning: json_decode() expects parameter 1 to be string, array given in

Sinnbeck's avatar

@basirafeef As I said, it is already an array (according to the error). So show what the output of dd($bodydata); is

basirafeef's avatar

@Sinnbeck sir, when i get $bodydata = $request->getBody()->getContents(); now the body data is : $bodydata = '[ { "name":"name 1", "age":18 }, { "name":"name 2", "age":19 }, { "name":"name 3", "age":18 } ]';

i decode the body data $result = User::insert(json_decode($bodydata, true));

Slim Application Error The application could not run because of the following error:

Details Type: InvalidArgumentException Code: 0 Message: Invalid HTTP status code. File: C:\wamp64\www\vendor\slim\psr7\src\Response.php Line: 191 Trace #0 C:\wamp64\www\vendor\slim\psr7\src\Response.php(153): Slim\Psr7\Response->filterStatus('HY000') #1 C:\wamp64\www\vendor\slim\http\src\Response.php(191): Slim\Psr7\Response->withStatus('HY000', '') #2 C:\wamp64\www\public\api\index.php(46): Slim\Http\Response->withStatus('HY000') #3 C:\wamp64\www\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(127): {closure}(Object(Slim\Http\ServerRequest), Object(Illuminate\Database\QueryException), true, true, true) #4 C:\wamp64\www\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(109): Slim\Middleware\ErrorMiddleware->handleException(Object(Slim\Http\ServerRequest), Object(Illuminate\Database\QueryException)) #5 C:\wamp64\www\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Http\ServerRequest), Object(class@anonymous)) #6 C:\wamp64\www\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(107): class@anonymous->handle(Object(Slim\Http\ServerRequest)) #7 C:\wamp64\www\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Http\ServerRequest), Object(class@anonymous)) #8 C:\wamp64\www\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Http\ServerRequest)) #9 C:\wamp64\www\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest)) #10 C:\wamp64\www\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(Slim\Http\ServerRequest)) #11 C:\wamp64\www\public\api\index.php(621): Slim\App->run() #12 {main}

Sinnbeck's avatar

@basirafeef Ah ok. So you got it to insert then? The error is a slim error and not related to eloquent.

1 like
basirafeef's avatar

@Sinnbeck thanks for your time and replies sir. $bodydata = $request->getParsedBody(); $bodydata is already json_encoded(); and the result is an array.

getType($bodydata ); is an array

when i pass this array to Eloquent Model User::Insert($Bodydata); the above error happens .

Sinnbeck's avatar

@basirafeef But none of the shown error has any mention of eloquent, so its kinda hard to help with it. Can you show exactly what $bodydata container? What you have shown so far is json, but you say its already an array :)

1 like
Sinnbeck's avatar

@basirafeef Sorry I have never used slim framework before :) But check the database if it was inserted

1 like
basirafeef's avatar

thanks sir the problem was in database structure, some columns does not have default values and i did not mentioned in json. when i added the columns to json it worked perfectly.

Please or to participate in this conversation.