quadrubo / filament-model-settings
这是我的软件包filament-model-settings
Requires
- php: ^8.1
- filament/filament: ^3.0
- glorand/laravel-model-settings: ^6.0|^7.0
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9|^8.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10.0.17|^11.0
README
此软件包利用glorand/laravel-model-settings将模型特定设置集成到Filament中。例如,您可以为Filament应用程序中的每个用户实现个性化设置。
安装
您可以通过composer安装此软件包
composer require quadrubo/filament-model-settings
可选,您可以使用以下命令发布视图
php artisan vendor:publish --tag="filament-model-settings-views"
用法
您应该首先设置您的eloquent模型。
重要:您应该阅读glorand/laravel-model-settings
的说明以了解如何操作。
单独的设置页面
然后您可以从生成设置页面开始。
php artisan make:filament-model-settings-page ManagePreferences
在您在app/Filament/{Panel}/Pages
目录中生成的新的设置页面类中,您应该填写getSettingsRecord()
函数。例如,要实现特定于用户的设置,只需返回当前活动的用户即可
public static function getSettingRecord() { return auth()->user(); }
您还应该编辑form()
函数以创建设置的字段。例如,如果您有设置theme
,您可以这样做
public function form(Form $form): Form { return $form ->schema([ Forms\Components\Select::make('theme') ->options([ 'dark' => 'Dark Mode', 'light' => 'Light Mode', 'high_contrast' => 'High Contrast', ]), ]); }
在用户菜单中使用页面
如果您想在此页面上使用filaments用户菜单,您可以在面板提供者中创建一个条目。
use App\Filament\Admin\Pages\ManagePreferences; class AdminPanelProvider extends PanelProvider { public function panel(Panel $panel): Panel { return $panel -> ... ->userMenuItems([ MenuItem::make() ->label('Settings') ->url(fn (): string => ManagePreferences::getUrl()) ->icon('heroicon-o-cog-6-tooth'), ]); } }
您还可能想隐藏侧边栏中的页面。
namespace App\Filament\Admin\Pages; class ManagePreferences extends ModelSettingsPage implements HasModelSettings { public static function shouldRegisterNavigation(): bool { return false; } }
您现有资源中的设置
设置也可以用于您现有的资源。例如,如果您有一个具有设置color
和can_add_students
的学校模型。
namespace App\Models; use Glorand\Model\Settings\Traits\HasSettingsField; class School extends Model { use HasSettingsField; public $defaultSettings = [ 'color' => '#ff0000', 'can_add_students' => true, ]; }
然后您可以使用提供的宏isModelSetting()
在资源中使用这些设置。
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(), ]); } }
如果在设置中更改了列名,您应将其提供给isModelSetting
作为前缀。
Forms\Components\Toggle::make('school_stuff.can_add_students') ->isModelSetting('school_stuff'),
测试
composer test
灵感
此软件包在很大程度上受到官方Spatie Laravel Settings Plugin的启发,并且基本上只是对glorand/laravel-model-settings进行了一些更改以使其兼容。
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略。
致谢
许可协议
MIT许可(MIT)。有关更多信息,请参阅许可文件。