Skip to content

Instantly share code, notes, and snippets.

@drjonnicholson
Last active June 6, 2020 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drjonnicholson/c209fc4083d7a1b27947fbb737abe218 to your computer and use it in GitHub Desktop.
Save drjonnicholson/c209fc4083d7a1b27947fbb737abe218 to your computer and use it in GitHub Desktop.
Laravel 5.3 BulkInsert Trait
<?php
namespace App\Traits;
use Carbon\Carbon;
use DB;
/*
* Original idea from: https://github.com/laravel/framework/issues/1295#issuecomment-193025045
*/
trait BulkInsert
{
/**
* Insert each item as a row. Does not generate events.
*
* @param array $items
*
* @return bool
*/
public static function insertAll(array $items)
{
$now = Carbon::now();
$items = collect($items)->map(function (array $data) use ($now) {
return array_merge([
'created_at' => $now,
'updated_at' => $now,
], $data);
})->all();
return DB::table((new self)->getTable())->insert($items);
}
}
@risman94
Copy link

dfgdfgdfg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment