Skip to content

Commit

Permalink
feat: settings on resources (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quadrubo committed Jan 9, 2024
1 parent f839935 commit 43c448c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
52 changes: 51 additions & 1 deletion README.md
Expand Up @@ -26,6 +26,8 @@ php artisan vendor:publish --tag="filament-model-settings-views"
You should start by setting up your eloquent model.
**Important:** You should read the [Instructions](https://github.com/glorand/laravel-model-settings#update_models) of the `glorand/laravel-model-settings` to find out how to do this.

### Seperate Settings Page

Then you can start by generating a settings page.

```bash
Expand Down Expand Up @@ -59,7 +61,7 @@ public function form(Form $form): Form
}
```

### Using the Page in the user menu
#### Using the Page in the user menu

If you want to use this page in filaments user menu, you can create an entry in your panel provider.

Expand Down Expand Up @@ -97,6 +99,54 @@ class ManagePreferences extends ModelSettingsPage implements HasModelSettings
}
```

### Settings within your existing Resouce

The settings can also be used in your existing resource.
If, for example you have a school model with the settings `color` and `can_add_students`.

```php
namespace App\Models;

use Glorand\Model\Settings\Traits\HasSettingsField;

class School extends Model
{
use HasSettingsField;

public $defaultSettings = [
'color' => '#ff0000',
'can_add_students' => true,
];
}
```

You can then use the provided macro `isModelSetting()` to use these settings inside your resource.

```php
namespace App\Filament\Resources;

class SchoolResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\ColorPicker::make('settings.color_1')
->isModelSetting(),
Forms\Components\Toggle::make('settings.can_add_students')
->isModelSetting(),
]);
}
}
```

In case you changed your column name for the settings, you should provide that to `isModelSetting` as a prefix.

```php
Forms\Components\Toggle::make('school_stuff.can_add_students')
->isModelSetting('school_stuff'),
```

## Testing

```bash
Expand Down
4 changes: 4 additions & 0 deletions src/FilamentModelSettingsServiceProvider.php
Expand Up @@ -2,6 +2,7 @@

namespace Quadrubo\FilamentModelSettings;

use Filament\Forms\Components\Field;
use Illuminate\Filesystem\Filesystem;
use Livewire\Features\SupportTesting\Testable;
use Quadrubo\FilamentModelSettings\Commands\MakeModelSettingsPageCommand;
Expand Down Expand Up @@ -45,6 +46,9 @@ public function packageBooted(): void
}
}

// Macros
Field::macro('isModelSetting', app(\Quadrubo\FilamentModelSettings\Macros\IsModelSetting::class)());

// Testing
Testable::mixin(new TestsFilamentModelSettings());
}
Expand Down
35 changes: 35 additions & 0 deletions src/Macros/IsModelSetting.php
@@ -0,0 +1,35 @@
<?php

namespace Quadrubo\FilamentModelSettings\Macros;

use Filament\Forms\Components\Field;

/**
* @param string $prefix
*
* @mixin \Filament\Forms\Components\Field;
*
* @return \Filament\Forms\Components\Field;
*/
class IsModelSetting
{
public function __invoke()
{
return function (string $prefix = 'settings') {
/** @var \Filament\Forms\Components\Field $this */
$this->afterStateHydrated(function (Field $component, $state) use ($prefix) {
$statePath = $component->getStatePath(false);

if (str_starts_with($statePath, $prefix)) {
$statePath = substr($statePath, strlen($prefix) + 1);
}

if ($state === null && $component->getRecord() !== null) {
$component->state($component->getRecord()->settings()->get($statePath));

Check failure on line 28 in src/Macros/IsModelSetting.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::settings().
}
});

return $this;
};
}
}

0 comments on commit 43c448c

Please sign in to comment.