visanduma / nova-settings
Laravel Nova 的高级设置 UI
1.0.1
2024-06-24 07:07 UTC
Requires
- php: ^7.3|^8.0
This package is auto-updated.
Last update: 2024-09-24 07:52:40 UTC
README
⚙ Nova 设置
使用原生字段为 Laravel Nova™ 仪表板提供设置管理 UI
💠 安装
composer require visanduma/nova-settings
💠 配置
发布配置和迁移
php artisan vendor:publish --tag=nova-settings
运行迁移
php artisan migrate
更新配置文件(可选)
<?php return [ /* default path for load settings class */ 'settings_path' => app_path('Nova/Settings'), ];
在模型中使用 trait。通常在 User.php
中
use Visanduma\NovaSettings\HasNovaSettings; class User extends Authenticatable { use HasNovaSettings; }
在 Nova 服务提供者中注册工具
public function tools() { return [ /* other tools */ \Visanduma\NovaSettings\NovaSettings::make(), ]; }
💠 创建第一个设置类
您可以使用 nova-settings:make
命令来构建一个新的设置类
php artisan nova-settings:make Contact
添加字段与 Nova 资源中的相同
class Contact extends NovaSettingsMum { public $icon = 'phone'; public function fields() { return [ Text::make('Name')->rules('required'), Text::make('Address')->rules('required'), Text::make('City')->rules('required'), Select::make('Province')->options([ 'NC' => 'North Central', 'N' => 'Northern', ]), Country::make('Country')->rules('required'), Panel::make('Home', [ Text::make('Contact name'), Text::make('Phone'), ])->help('Update you home contacts'), ]; } }
💠 注册设置
默认路径下的所有设置类都会自动注册。如果您打算使用不同的路径,请在 nova-settings.php
中进行配置
如果您想手动注册设置类,请在服务提供者中使用 NovaSettings::register
方法
namespace App\Nova\Settings; public function boot() { NovaSettings::register([ Contact::class, ]); }
💠 自定义设置类
您可以根据需要自定义设置类
自定义设置菜单图标。您可以使用任何 Heroicons
public $icon = 'bell';
自定义部分标签
public function label():string { return 'User contacts'; }
自定义 uriKey。当保存/检索设置时使用 uriKey
public function uriKey(): string { return 'user-contacts'; }
💠 模型设置与全局设置
有两种类型的设置。 模型设置 和 全局设置。模型设置始终绑定到一个实体(默认为认证用户),而全局设置则不绑定到任何实体
您可以通过设置类中的 global
属性轻松配置设置类型
protected bool $global = false;
如果您想使用除 User 模型之外的另一个模型,只需在设置类中覆盖 getModel()
方法即可
protected function getModel() { return Auth::user(); // default model is Auth User }
💠 获取设置
use Visanduma\NovaSettings\NovaSettings; // getting single value NovaSettings::get('contact.name', 'default value'); nova_settings('contact.name','default value'); // getting whole settings array 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'); NovaSettings::global('system'); nova_settings_global('system.phone'),
💠 转换输入
如果您需要自定义用户提交的表单数据,请在设置类中覆盖以下方法。此方法将接收表单数据数组作为其参数
protected function transformInputs(array $inputs): array { // do your modifications return $inputs; }
💠 钩子
protected function afterSaved(NovaRequest $request) { // this method will be called after the form is saved }
已知问题
- 文件输入无法正常工作
- 上传的文件无法删除
- 与第三方字段不兼容
待办事项
- 授权
- 缓存
- 事件
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件