@vincentsanity Can you dump what $trimmed contains?
Or also try moving the DB::insert statement inside this loop
foreach ($trimmed as $data){
$inserts[] = ...
\DB::table('property_site_plan')->insert($inserts);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I got a 3rd party api and I want to save data when I hit a request. It has a small data but there is one column that has lots of text in it. So I set the column of that object in the database to text. The problem is when I hit the api it got me this error of PDOStatement::execute(): MySQL server has gone away everytime i insert
Here is my code
public function propertySitePlans(){
ini_set('max_execution_time', 600);
ini_set('max_allowed_packet',500);
ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);
ini_set('mysqli.reconnect',true);
ini_set('wait_timeout',36000);
$project_id = \DB::table('project_list')
->select('projectId')
->get();
foreach($project_id as $res){
$request_time = Carbon::now()->format('YmdHis');
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$sign = md5($token.$request_time);
$url = 'https://cgi.singmap.com/project/querySitePlans?request_time='.$request_time.
'&token='.$token.'&sign='.$sign.'&projectId='.$res->projectId.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
$trimmed = $response['datas'];
// dd($trimmed);
foreach ($trimmed as $data){
$inserts[] = [
'projectId' => $res->projectId,
'buildingId' => $data['buildingId'],
'showInterest' => $data['showInterest'],
'showIndex' => $data['showIndex'],
'sitePlanName' => $data['sitePlanName'],
'img' => $data['img'],
'showInMobile' => $data['showInMobile'],
'sitePlanId' => $data['sitePlanId'],
'type' => $data['type'],
'content' => $data['content'],
];
}
}
\DB::table('property_site_plan')->insert($inserts);
dd('Data Inserted');
}
Im using mysql
Ver 15.1 Distrib 10.3.16-MariaDB, for Win64 (AMD64), source revision 0789a1a18f0e780c0412667e7b6e0a9970aa6905
@vincentsanity make sure that you have the right database configuration in .env file. If you sure that you have a proper connection but the problem is happening for another reason. Then try to raising max_allowed_packet in my.cnf (under [mysqld]) to 8 or 16M usually fixes it.
[mysqld]
max_allowed_packet=16M
NOTE: This can be set on your server as it's running. You need to restart the MySQL service once you are done.
After all of this if the error still happening or if you don't able to edit my.cnf then give a try this one.
foreach ($trimmed as $data){
$inserts = [
'projectId' => $res->projectId,
'buildingId' => $data['buildingId'],
'showInterest' => $data['showInterest'],
'showIndex' => $data['showIndex'],
'sitePlanName' => $data['sitePlanName'],
'img' => $data['img'],
'showInMobile' => $data['showInMobile'],
'sitePlanId' => $data['sitePlanId'],
'type' => $data['type'],
'content' => $data['content'],
];
\DB::table('property_site_plan')->insert($inserts);
}
}
Please or to participate in this conversation.