Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit 8695a09

Browse files
Merge pull request #17 from DarkGhostHunter/master
Fixes not-raw SQL statement for migrations #16
2 parents b427aeb + 890cf1a commit 8695a09

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

README.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -427,22 +427,7 @@ Laraconfig uses one single bag called `default`. If you have declared in the man
427427
428428
```php
429429
// app/Models/User.php
430-
431-
/**
432-
* Returns the bags this model uses for settings.
433-
*
434-
* @return array|string
435-
*/
436-
public function getSettingsBags(): array|string
437-
{
438-
$bags = ['notifications'];
439-
440-
if ($this->is_premium) {
441-
$bags[] = 'theme';
442-
}
443-
444-
return $bags;
445-
}
430+
i
446431
```
447432
448433
The above will apply a filter to the query when retrieving settings from the database. This makes easy to swap bags when a user has a different role or property, or programmatically.

src/Migrator/Pipes/CreateNewMetadata.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Console\OutputStyle;
1111
use Illuminate\Support\Carbon;
1212
use Illuminate\Support\Collection;
13+
use Illuminate\Support\Facades\DB;
1314

1415
/**
1516
* @internal
@@ -145,13 +146,14 @@ protected function fillSettings(Metadata $metadata, Collection $models): int
145146
foreach ($models as $model) {
146147
$affected += Setting::query()->insertUsing(
147148
['metadata_id', 'settable_id', 'settable_type', 'value', 'created_at', 'updated_at'],
148-
$model->newQuery()->select([
149-
$metadata->getKey(). ' as metadata_id',
150-
$model->getKeyName(). ' as settable_id',
151-
$model->getMorphClass(). ' as settable_type',
152-
$metadata->getRawOriginal('default', 'NULL').' as value',
153-
$this->now->toDateTimeString(). ' as created_at',
154-
$this->now->toDateTimeString(). ' as updated_at',
149+
$model->newQuery()
150+
->select([
151+
DB::raw("'{$metadata->getKey()}' as metadata_id"),
152+
DB::raw("{$model->getKeyName()} as settable_id"),
153+
DB::raw("'{$model->getMorphClass()}' as settable_type"),
154+
DB::raw("'{$metadata->getRawOriginal('default', 'NULL')}' as value"),
155+
DB::raw("'{$this->now->toDateTimeString()}' as created_at"),
156+
DB::raw("'{$this->now->toDateTimeString()}' as updated_at"),
155157
])->getQuery()
156158
);
157159
}
@@ -175,12 +177,12 @@ protected function copySettings(Metadata $new, Metadata $old): int
175177
['metadata_id', 'settable_id', 'settable_type', 'value', 'created_at', 'updated_at'],
176178
Setting::query()->where('metadata_id', $old->getKey())
177179
->select([
178-
$new->getKey(). ' as metadata_id',
180+
DB::raw("'{$new->getKey()}' as metadata_id"),
179181
'settable_id',
180182
'settable_type',
181183
'value', // Here we will just instruct to copy the value raw to the new setting.
182-
$this->now->toDateTimeString(). ' as created_at',
183-
$this->now->toDateTimeString(). ' as updated_at',
184+
DB::raw("'{$this->now->toDateTimeString()}' as created_at"),
185+
DB::raw("'{$this->now->toDateTimeString()}' as updated_at"),
184186
])->getQuery()
185187
);
186188
}
@@ -216,4 +218,4 @@ protected function migrateSettings(Declaration $declaration, Metadata $new, Meta
216218

217219
return $affected;
218220
}
219-
}
221+
}

0 commit comments

Comments
 (0)