0

I have an activities log tables, And I want to batch write to log tables each time system collected more than 100 records. How to do that with Laravel ?

Thank you!

Tho Bui Ngoc
  • 763
  • 1
  • 11
  • 36
  • Could you explain why you want to do it this way? Why not just insert them as they come in, one by one? – ceejayoz Nov 18 '19 at 04:15
  • Hello @ceejayoz, I want to save write database cost & optimize performance for system. – Tho Bui Ngoc Nov 18 '19 at 04:17
  • 1
    This adds a lot of complexity and risk of lost records for minimal performance benefit. It's likely there are much better places to work on performance, and that this is premature optimization. – ceejayoz Nov 18 '19 at 04:19

1 Answers1

0

You can do that as below...

$data = array(
array(
    'log_name'=>'Syntax',
    'type'=>'Error',
    'created_at'=>date('Y-m-d H:i:s'),
    'modified_at'=> date('Y-m-d H:i:s')
   ),
array(
     'log_name'=>'Logical',
     'type'=>'Warning',
     'created_at'=>date('Y-m-d H:i:s'),
     'modified_at'=> date('Y-m-d H:i:s')
   ),
//...
);
Log::insert($data);

Or using query builder as

DB::table('log')->insert($data);
Suraj15689
  • 29
  • 4