thiktak/filament-sql-nested-builder-form

FilamentPHP 插件 - SQL 嵌套构建表单组件

v0.1.0-alpha 2023-09-14 22:02 UTC

This package is auto-updated.

Last update: 2024-09-10 01:18:58 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

thiktak/filament-nested-builder-form 的应用,用于 SQL,支持(不)AND/OR 和子组。

警告

请注意,此包尚未推荐用于生产。请帮助我们进行测试和反馈 :)

安装

您可以通过 composer 安装此包

composer require thiktak/filament-sql-nested-builder-form

此包基于 thiktak/filament-nested-builder-form

用法

// use App\Models\User;
use Thiktak\FilamentNestedBuilderForm\Forms\Components\NestedBuilder;
use Thiktak\FilamentNestedBuilderForm\Forms\Components\NestedSubBuilder;
use Thiktak\FilamentSQLNestedBuilderForm\Forms\Components\SQLNestedBuilder;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([

                Section::make('Nested Builder Form')
                    ->description('Example of the SQL Nested Builder Form (SQL Query)')
                    ->schema([
                        // configuration is an Array
                        SQLNestedBuilder::make('configuration')

                            // nestedConfiguration apply this set up to all children
                            ->nestedConfiguration(function (NestedSubBuilder $builder, NestedBuilder $parent) {
                                // import default configuration of this package
                                $parent->defaultNestedConfiguration($builder);
                            })

                            // Display the first Hint as a Raw SQL query of User Model
                            ->hint(function (?array $state) {
                                return SQLNestedBuilder::getFullyLinearizedArrayToEloquent($state, User::query())
                                    ->getQuery()
                                    ->toRawSql()
                                ;
                            })
                    ]),
            ]);
    }

配置

更改字段输入(字段的名称)

使用嵌套配置并设置 fieldComponent

    SQLNestedBuilder::make('configuration')

        ->nestedConfiguration(function (NestedSubBuilder $builder, NestedBuilder $parent) {
            // import default configuration of this package
            $parent->defaultNestedConfiguration($builder);

            // Change the TextInput -> Select
            $parent->fieldComponent(
                fn () => Select::make('field')
                    ->options([
                        'id'    => 'User Id',
                        'email' => 'User email',
                    ])
                    ->searchable()
            );
        })

导出到 SQL(字符串)

此方法将返回用户模型查询的原始 SQL。$state 是数据数组。

SQLNestedBuilder::getFullyLinearizedArrayToEloquent($state, User::query())
    ->getQuery()
    ->toRawSql()

输出(示例)

select *
  from `users`
  where (
    (
      not (
        (`a` = '1') and (`b` in ('2', '3'))
        and (`a` between '1' and '99')
      )
      or (`email` LIKE '%admin.com%')
      or (`email` LIKE 'a%')
      or (`email` LIKE '%com')
    )
    and (`tenant_id` = '1')
  )

导出到 Eloquent

  SQLNestedBuilder::getFullyLinearizedArrayToSQL(?array $state); // If consume the whole array

  SQLNestedBuilder::getFullyLinearizedArrayToSQL(?array $state, 'group'); // if level of group
  SQLNestedBuilder::getFullyLinearizedArrayToSQL(?array $state, 'rule'); // if level of rule

输出(示例)

(`a` = '1' AND `b` IN ('2', '3') AND `a` BETWEEN '1' AND '99') AND `email` LIKE '%admin.com%' AND `email` LIKE 'a%' AND `email` LIKE '%com'

添加自定义运算符

(当前解决方案)扩展 SQLNesterBuilder,并重新定义方法 loadDefinition(),调用 $this->registerOperator(MyOperator::class)

更新日志

请参阅 CHANGELOG 了解最近更改的更多信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请审查 我们的安全策略 了解如何报告安全漏洞。

致谢

许可

MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。