1

I'm sending data with a ajax call to my laravel server at http://127.0.0.1:8000/data

Laravel Route

Route::post('/data',[MainController::class, 'data']);

my modle

 public function up()
{
    Schema::create('names', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->timestamps();
    });
}

Controller

   function data(Request $request)
{

$requestPayload = file_get_contents("php://input");
$object = json_decode($requestPayload);
return ($object);

}

Json data i want to save in database

[ { "name": "Monika khrolia" }, { "name": "Ranjana" }, { "name": "Ramesh Kumar" }, { "name": "Ansuman Biswas" }, { "name": "Avijit Ghosh" }, { "name": "RAHUL.R" }, { "name": "ITI Shitoley" }, { "name": "Shubham soni" }, { "name": "Awanish Ranjan Rai" }, { "name": "Neetu Sharma" }, { "name": "Rishi Raj" }, { "name": "Priyanka Pokharkar" }, { "name": "Chetan Mistry" }, { "name": "Chiman Mandali" }, { "name": "Raka Yadav" }, { "name": "Munna Kumar" }, { "name": "kayum ansari" }, { "name": "Meraj Ahmad Naqui" }, { "name": "Hem Kumar Sharma" }, { "name": "Shantangsu Kumar Das" }, { "name": "Sreenivasreddy" }, { "name": "Mahendra Kumar" }, { "name": "Pragya Singhal" }, { "name": "Khushboo Talwar" }, { "name": "Milan Gayen" }, { "name": "Ankita Pandey" }, { "name": "Nooruddin Saheb" }, { "name": "S.JAHIR HUSSAIN" }, { "name": "Arunraj Tamilselvam Harijan" }, { "name": "Kunal Murlidhar Kamble" }, { "name": "ADYASHA BEJ" }, { "name": "Rajendra Yuvraj More" }, { "name": "sunil" }, { "name": "Sudipta Dey" }, { "name": "raman prakash" }, { "name": "Harender" }, { "name": "Sk Tambirul Islam" }, { "name": "Ravi yadav" }, { "name": "Maneesh Singh" }, { "name": "Navneet Chauhan" } ]

3
  • This is basic Laravel stuff. Look into "Eloquent Inserts" laravel.com/docs/8.x/eloquent#inserts. And you can probably just grab it from the $request. Commented Sep 10, 2021 at 13:30
  • show us the code where you are actually saving the data? If it's just the JSON data you want to save then parse it in an array and call a simple eloquent Insert/update method with the data. stackoverflow.com/a/49568523/6869708 & stackoverflow.com/a/48128541/6869708 Commented Sep 10, 2021 at 13:38
  • Thanks @RohitS for your help Commented Sep 10, 2021 at 13:41

1 Answer 1

0

You have several options to deal with this (documentation):

  • You can do a bulk insert
DB::table( 'names' )->insert( object );

or

DB::table( 'names' )->insertOrIgnore( object );
  • Or you can do the save it using the model (in case you want to validate o whatever) (documentation):
foreach( $objectList as $objectItem ) 
{
    $name = Name::firstOrNew( [ 'name' => $objectItem->name ]);
    $name->save();
}

Best,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.