From c7b2159609c1a108d3695afbd78ccbc23e989db3 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Mon, 26 Jul 2021 17:29:53 -0400 Subject: [PATCH 1/2] May have fixed complex SQL queries for migrations. --- src/Migrator/Pipes/CreateNewMetadata.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Migrator/Pipes/CreateNewMetadata.php b/src/Migrator/Pipes/CreateNewMetadata.php index c0be7d2..ee647ad 100644 --- a/src/Migrator/Pipes/CreateNewMetadata.php +++ b/src/Migrator/Pipes/CreateNewMetadata.php @@ -10,6 +10,7 @@ use Illuminate\Console\OutputStyle; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\DB; /** * @internal @@ -145,13 +146,14 @@ protected function fillSettings(Metadata $metadata, Collection $models): int foreach ($models as $model) { $affected += Setting::query()->insertUsing( ['metadata_id', 'settable_id', 'settable_type', 'value', 'created_at', 'updated_at'], - $model->newQuery()->select([ - $metadata->getKey(). ' as metadata_id', - $model->getKeyName(). ' as settable_id', - $model->getMorphClass(). ' as settable_type', - $metadata->getRawOriginal('default', 'NULL').' as value', - $this->now->toDateTimeString(). ' as created_at', - $this->now->toDateTimeString(). ' as updated_at', + $model->newQuery() + ->select([ + DB::raw("'{$metadata->getKey()}' as metadata_id"), + DB::raw("{$model->getKeyName()} as settable_id"), + DB::raw("'{$model->getMorphClass()}' as settable_type"), + DB::raw("'{$metadata->getRawOriginal('default', 'NULL')}' as value"), + DB::raw("'{$this->now->toDateTimeString()}' as created_at"), + DB::raw("'{$this->now->toDateTimeString()}' as updated_at"), ])->getQuery() ); } @@ -175,12 +177,12 @@ protected function copySettings(Metadata $new, Metadata $old): int ['metadata_id', 'settable_id', 'settable_type', 'value', 'created_at', 'updated_at'], Setting::query()->where('metadata_id', $old->getKey()) ->select([ - $new->getKey(). ' as metadata_id', + DB::raw("'{$new->getKey()}' as metadata_id"), 'settable_id', 'settable_type', 'value', // Here we will just instruct to copy the value raw to the new setting. - $this->now->toDateTimeString(). ' as created_at', - $this->now->toDateTimeString(). ' as updated_at', + DB::raw("'{$this->now->toDateTimeString()}' as created_at"), + DB::raw("'{$this->now->toDateTimeString()}' as updated_at"), ])->getQuery() ); } @@ -216,4 +218,4 @@ protected function migrateSettings(Declaration $declaration, Metadata $new, Meta return $affected; } -} \ No newline at end of file +} From 890cf1a3520c181cba1bd898250e8cbf4365ef5c Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Mon, 26 Jul 2021 19:22:36 -0400 Subject: [PATCH 2/2] Fixes code example for bag filtering. --- README.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/README.md b/README.md index 812958e..d05ee52 100644 --- a/README.md +++ b/README.md @@ -427,22 +427,7 @@ Laraconfig uses one single bag called `default`. If you have declared in the man ```php // app/Models/User.php - -/** - * Returns the bags this model uses for settings. - * - * @return array|string - */ -public function getSettingsBags(): array|string -{ - $bags = ['notifications']; - - if ($this->is_premium) { - $bags[] = 'theme'; - } - - return $bags; -} +i ``` 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.