From 8a809d77bf50b52c2e947713e79894610587d177 Mon Sep 17 00:00:00 2001 From: LaHiRu Date: Mon, 24 Jun 2024 12:36:14 +0530 Subject: [PATCH] custom model --- Readme.md | 20 +++++++++-- resources/stubs/user_settings.stub | 2 +- src/NovaSettings.php | 13 +++++-- src/NovaSettingsMum.php | 54 ++++++++++++++++++++---------- 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/Readme.md b/Readme.md index 4c53853..5737fb2 100644 --- a/Readme.md +++ b/Readme.md @@ -186,10 +186,10 @@ Customizing uriKey. `uriKey` is used when saving/retrieving the settings
-### 💠 User settings vs Global settings +### 💠 Model settings vs Global settings -There are two type of settings. **User settings** & **Global Settings**. -User settings is always bound to an entity (Generally for User Model) while global settings are not bound to any entity +There are two type of settings. **Model settings** & **Global Settings**. +Model settings is always bound to an entity (Auth user by default) while global settings are not bound to any entity You can easily configure the settings type with `global` property in the settings class @@ -197,6 +197,15 @@ You can easily configure the settings type with `global` property in the setting protected bool $global = false; ``` +if you want use another Model rather than User Model , just override the `getModel()` method on settings class + +```php +protected function getModel() +{ + return Auth::user(); // default model is Auth User +} +``` +
### 💠 Retrieving the settings @@ -214,6 +223,11 @@ NovaSettings::get('contact'); nova_settings('contact'); +// use different Model instead Auth User +NovaSettings::get('contact.name', 'default value', Admin::find(3)); + +nova_settings('contact.name','default value', Admin::find(3)); + // getting global settings NovaSettings::global('system.email'); diff --git a/resources/stubs/user_settings.stub b/resources/stubs/user_settings.stub index bd81f64..5c32d54 100644 --- a/resources/stubs/user_settings.stub +++ b/resources/stubs/user_settings.stub @@ -10,7 +10,7 @@ class $CLASS_NAME$ extends NovaSettingsMum { public $icon = 'cog'; - public function fields() + public function fields():array { return [ Text::make('Name')->rules('required'), diff --git a/src/NovaSettings.php b/src/NovaSettings.php index 6efce37..50e4213 100644 --- a/src/NovaSettings.php +++ b/src/NovaSettings.php @@ -94,14 +94,21 @@ public static function findSection($name): NovaSettingsMum return static::keyByUri()[$name] ?? null; } - public static function get($key, $default = null) + public static function get($key, $default = null, $model = null) { + $model = $model ?? Auth::user(); + + throw_unless( + in_array(HasNovaSettings::class, class_uses_recursive($model)), + 'Model should be use HasNovaSettings trait' + ); + $key = str($key)->trim('.'); if ($key->contains('.')) { - return Auth::user()->novaSettings()->where('key', $key)->first()?->value ?? $default; + return $model->novaSettings()->where('key', $key)->first()?->value ?? $default; } else { - $results = Auth::user()->novaSettings()->where('key', 'LIKE', "$key.%") + $results = $model->novaSettings()->where('key', 'LIKE', "$key.%") ->get() ->map(function ($el) { $el['key'] = str($el->key)->after('.')->toString(); diff --git a/src/NovaSettingsMum.php b/src/NovaSettingsMum.php index 18d978b..923237a 100644 --- a/src/NovaSettingsMum.php +++ b/src/NovaSettingsMum.php @@ -5,7 +5,7 @@ use Illuminate\Http\Response; use Illuminate\Http\UploadedFile; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\Auth; use Laravel\Nova\Fields\File; use Laravel\Nova\Http\Requests\NovaRequest; use Visanduma\NovaSettings\Models\NovaSettingsModel; @@ -14,12 +14,12 @@ abstract class NovaSettingsMum { protected bool $global = false; - public function fields() + public function fields(): array { return []; } - public function label():string + public function label(): string { return str(get_called_class()) ->afterLast('\\') @@ -81,8 +81,8 @@ public function store(NovaRequest $request): Response // save global settings return $this->saveGlobalSettings($data, $request); } else { - // save settings for current auth user - return $this->saveUserSettings($data, $request); + // save settings for model + return $this->saveModelSettings($data, $request); } // call after hooks @@ -96,16 +96,31 @@ protected function transformInputs(array $inputs): array return $inputs; } - private function saveUserSettings(Collection $data, NovaRequest $request) + /** + * @return Illuminate\Contracts\Auth\Authenticatable | Illuminate\Database\Eloquent\Model + */ + protected function getModel() { - $data->each(function ($value, $key) use ($request) { - $request->user()->novaSettings()->updateOrCreate( - [ - 'key' => $key, - ], [ - 'value' => $value, - ] - ); + return Auth::user(); + } + + private function saveModelSettings(Collection $data, NovaRequest $request) + { + throw_unless( + in_array(HasNovaSettings::class, class_uses_recursive($this->getModel())), + 'Model should be use HasNovaSettings trait' + ); + + $data->each(function ($value, $key) { + $this->getModel() + ->novaSettings() + ->updateOrCreate( + [ + 'key' => $key, + ], [ + 'value' => $value, + ] + ); }); return response('', 204); @@ -131,13 +146,18 @@ public function getSettings(NovaRequest $request) if ($this->global) { return NovaSettings::global($this->uriKey()); } else { - return $this->getUserSettings($request->user()); + return $this->getModelSettings($this->getModel()); } } - public function getUserSettings($user) + public function getModelSettings($model) { - return $user->novaSettings() + throw_unless( + in_array(HasNovaSettings::class, class_uses_recursive($this->getModel())), + 'Model should be use HasNovaSettings trait' + ); + + return $model->novaSettings() ->where('key', 'LIKE', "{$this->uriKey()}.%") ->get() ->map(function ($el) {