How to save a JSON Object to MySQL Database in laravel

  ajax, json, laravel, php

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"
}
]

Source: Laravel

Leave a Reply