Skip to content

Commit

Permalink
Merge pull request #12 from Visanduma/model-detach
Browse files Browse the repository at this point in the history
use custom model
  • Loading branch information
lahirulhr authored Jun 24, 2024
2 parents d438d55 + 8a809d7 commit 1a41cd3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
20 changes: 17 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,26 @@ Customizing uriKey. `uriKey` is used when saving/retrieving the settings

<br>

### 💠 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

```php
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
}
```

<br>

### 💠 Retrieving the settings
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion resources/stubs/user_settings.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
13 changes: 10 additions & 3 deletions src/NovaSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
54 changes: 37 additions & 17 deletions src/NovaSettingsMum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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('\\')
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 1a41cd3

Please sign in to comment.