An application of thiktak/filament-nested-builder-form for SQL with (not) AND/OR and sub-groups.
Warning
Be careful, this package is not yet recommended for production. Help us with testing and feedback :)
You can install the package via composer:
composer require thiktak/filament-sql-nested-builder-form
This package is based on 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()
;
})
]),
]);
}
Use the nestedConfiguration and set up 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()
);
})
This method will return the Raw SQL of a User model query. $state is the data array.
SQLNestedBuilder::getFullyLinearizedArrayToEloquent($state, User::query())
->getQuery()
->toRawSql()
Output (example):
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')
)
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
Output (example):
(`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'
(current solution) Extend the SQLNesterBuilder, and redefine the method loadDefinition() with a call to $this->registerOperator(MyOperator::class)
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.