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

sahar_mkr's avatar

create method in laravel

is that true that when create method is being called within a loop, each call to create, overwrites the previous one, resulting in only the last record being saved? I have the collection ofdata like this: {{date=>17:00:04, response_time=> 465, server_referer=> iran, site_name=>x}, {date=>17:30:13, response_time=> 0, server_referer=> iran, site_name=>x}}, and I want to save it in a table, I have used this lines of code but just the last one saves in the table, it seems that it is updated row!!

if (!$responsetimeDashboard->isEmpty()) {
foreach ($responsetimeDashboard as $dashboard) {
    try {
        ModelsResponsetimeData::create (
            [
                'type'           => 'Dashboard',
                'site_name'      => $dashboard->site_name,
                'response_time'  => $dashboard->response_time,
                'server_referer' => $dashboard->server_referer,
                'date'           => $dashboard->date,
            ]
        );  

    } catch (\Exception $e) {
        echo $e->getMessage("Something went wrong");
    }  
}

}

0 likes
3 replies
sahar_mkr's avatar

@martinbean thanks for your suggestion, I have removed try/catch, and it seems that works, but can you explain why this happens with try/catch block? I have three create methods, and I needed to use try/catch in them. my whole code was and I removed tr/catch and DB::transactions:

 try {
         DB::beginTransaction();
         if (!$responseDashboard->isEmpty()) {
             foreach ($responseDashboard as $dashboard) {
   
                 ModelsResponsetimeData::create(
                     [
                         'type'           => 'Dashboard',
                         'site_name'      => $dashboard->site_name,
                         'response_time'  => $dashboard->response_time,
                         'server_referer' => $dashboard->server_referer,
                        'date'           => $dashboard->date,
                         'duration'       => 'day' // is additional
                     ]
                 );  
    
          
             }
        }

         if (!$responseApi->isEmpty()) {
            foreach ($responseApi as $api) { 
                 ModelsResponsetimeData::create(
                     [
                         'type'           => 'API',
                         'site_name'      => $api->site_name,
                         'response_time'  => $api->response_time,
                         'server_referer' => $api->server_referer,
                         'date'           => $api->date,
                         'duration'       => 'day' // is additional
                     ]
                 );  
    
             }
         }

         if (!$responsePay->isEmpty()) {
             foreach ($responsePay as $pay) {
                 ModelsResponsetimeData::create(
                     [
                         'type'           => 'Pay',
                         'site_name'      => $pay->site_name,
                         'response_time'  => $pay->response_time,
                         'server_referer' => $pay->server_referer,
                         'date'           => $pay->date,
                         'duration'       => 'day' // is additional
                    ]
                 );  
    
             }
         }
         DB::commit();
     } catch (\Throwable $th) {
         DB::rollback();
       return $e->getMessage();
     } 

Please or to participate in this conversation.