Skip to content

Commit

Permalink
Update DatabaseStore.php
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc authored Feb 25, 2021
1 parent c9c54b4 commit f161b09
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Stores/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Arcanedev\LaravelSettings\Utilities\Arr;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;

/**
* Class DatabaseStore
Expand Down Expand Up @@ -269,13 +270,16 @@ protected function newQuery(bool $insert = false): Builder
*/
protected function prepareInsertData(array $data): array
{
$now = Carbon::now();
$dbData = [];
$extraColumns = $this->extraColumns ? $this->extraColumns : [];

foreach ($data as $key => $value) {
$dbData[] = array_merge($extraColumns, [
$this->keyColumn => $key,
$this->valueColumn => $value,
$this->keyColumn => $key,
$this->valueColumn => $value,
$this->model->getCreatedAtColumn() => $now,
$this->model->getUpdatedAtColumn() => $now,
]);
}

Expand Down Expand Up @@ -341,12 +345,9 @@ private function syncUpdated(array $updated): void
private function syncInserted(array $inserted): void
{
if ( ! empty($inserted)) {
$preparedInsertData = $this->prepareInsertData($inserted);
$preparedInsertDataWithTimestamps = array_map(function ($insert) {
return array_merge(['created_at' => now(), 'updated_at' => now()], $insert);
}, $preparedInsertData);

$this->newQuery(true)->insert($preparedInsertDataWithTimestamps);
$this->newQuery(true)->insert(
$this->prepareInsertData($inserted)
);
}
}

Expand Down

0 comments on commit f161b09

Please sign in to comment.