It says that the unique constraint for your table primary key is violated. That means that you are trying to insert something that already exist in the table with that key.
If you show the complete sql error it's easier to tell you which field.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have an array that I need to save to Database but i have multiple issues when inserting it.
array:3 [▼
"status" => "success"
"data" => array:5 [▼
0 => array:11 [▼
"id" => "8c8c13b6-35ed-3ffb-92d5-c438825df67f"
"date_of_birth" => "1990-06-29"
"image" => "https://lorempixel.com/640/480/people/?96612"
"email" => "[email protected]"
"first_name" => "Dayni"
"last_name" => "Mayez"
"title" => "Mr."
"address" => "18342 Alisa Square Suite 259"
"country" => "USA"
"bio" => """
Maxime ratione optio ratione voluptatem sed rem rerum.
\t\t\t\t Eaque voluptatem veniam voluptatum porro. Animi sequi nisi ut
\t\t\t\t quia minima aspernatur. At enim consequuntur sunt blanditiis.
"""
"rating" => "3.0600000000000001"
]
1 => array:11 [▼
"id" => "8c2313b6-35ed-3ffb-92d5-c438825df67f"
"date_of_birth" => "1980-06-29"
"image" => "https://lorempixel.com/640/480/people/?96613"
"email" => "[email protected]"
"first_name" => "Alisa"
"last_name" => "Milesz"
"title" => "Mrs."
"address" => "774 Snider Street"
"country" => "USA"
"bio" => """
Maxime ratione optio ratione voluptatem sed rem rerum.
\t\t\t\t Eaque voluptatem veniam voluptatum porro. Animi sequi nisi ut
\t\t\t\t quia minima aspernatur. At enim consequuntur sunt blanditiis.
"""
"rating" => "3.0600000000000002"
]
2 => array:11 [▼
"id" => "1c2313b6-35ed-3ffb-92d5-c438825df67f"
"date_of_birth" => "1955-06-29"
"image" => "https://lorempixel.com/640/480/people/?96614"
"email" => "[email protected]"
"first_name" => "Andre"
"last_name" => "Barbuda"
"title" => "Mr."
"address" => "4593 Michigan Avenue"
"country" => "USA"
"bio" => """
Maxime ratione optio ratione voluptatem sed rem rerum.
\t\t\t\t Eaque voluptatem veniam voluptatum porro. Animi sequi nisi ut
\t\t\t\t quia minima aspernatur. At enim consequuntur sunt blanditiis.
"""
"rating" => "1.0600000000000001"
]
3 => array:11 [▼
"id" => "2c2313b6-35ed-3ffb-92d5-c438825df67f"
"date_of_birth" => "1986-06-29"
"image" => "https://lorempixel.com/640/480/people/?96616"
"email" => "[email protected]"
"first_name" => "James"
"last_name" => "Stein"
"title" => "Mr."
"address" => "Colorado Springs, CO 80903"
"country" => "USA"
"bio" => """
Maxime ratione optio ratione voluptatem sed rem rerum.
\t\t\t\t Eaque voluptatem veniam voluptatum porro. Animi sequi nisi ut
\t\t\t\t quia minima aspernatur. At enim consequuntur sunt blanditiis.
"""
"rating" => "5.0600000000000001"
]
4 => array:11 [▼
"id" => "3c2313b6-35ed-3ffb-92d5-c438825df67f"
"date_of_birth" => "1982-06-29"
"image" => "https://lorempixel.com/640/480/people/?96618"
"email" => "[email protected]"
"first_name" => "John"
"last_name" => "Tompkins"
"title" => "Mr."
"address" => "4451 Deans Lane"
"country" => "USA"
"bio" => """
Maxime ratione optio ratione voluptatem sed rem rerum.
\t\t\t\t Eaque voluptatem veniam voluptatum porro. Animi sequi nisi ut
\t\t\t\t quia minima aspernatur. At enim consequuntur sunt blanditiis.
"""
"rating" => "2.0600000000000001"
]
]
"msg" => ""
]
And my database structure is:
public function up()
{
Schema::create('Employees', function (Blueprint $table) {
$table->string('id');
$table->date('date_of_birth');
$table->string('image');
$table->string('email');
$table->string('first_name');
$table->string('last_name');
$table->string('title');
$table->string('address');
$table->string('country');
$table->text('bio');
$table->bigInteger('rating');
$table->timestamps();
});
}
in my controller i have this function:
public function Data()
{
$token = "b5846ddac7d27dd4960dcb749ff76642";
$client = new Client();
$response = $client->request('GET', 'url_for_the_api_i_am_getting_the_data_from', [
'headers' => [
'Access-Token' => $token,
'Content-Type' => 'application/json',
],
]);
$data = json_decode($response->getBody()->getContents(), true);
$timestamp = Carbon::now()->toDateTimeString();
$prepared = collect($data['data'])->map(function($item) use ($timestamp) {
$item['created_at'] = $timestamp;
$item['updated_at'] = $timestamp;
return $item;
});
Employee::insert($prepared->toArray());
}
When i try to insert the data to the Database i am getting
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8' for key 'PRIMARY' (SQL: insert into `employees`
Any Ideas how can i insert this array in DB or where i am making a mistake?
Thanks!
Thanks for the reply, but I wend for a different solution where I insert all the data with one insert instead of multiple entries
$data = json_decode($response->getBody()->getContents(), true);
$timestamp = Carbon::now()->toDateTimeString();
$prepared = collect($data['data'])->map(function($item) use ($timestamp) {
$employee = new Employee();
$employee->employee_id = $item['id'] ;
$item['created_at'] = $timestamp;
$item['updated_at'] = $timestamp;
return $item;
});
//insert in database
Employee::insert($prepared->toArray());
it works perfect!
Please or to participate in this conversation.