- Can I use alhames/filter-bundle in Laravel to auto-generate database columns like TINYINT(1) or FLOAT without writing raw SQL?
- Yes, the package dynamically generates SQL column definitions (e.g., `TINYINT(1)` for booleans) based on runtime configurations like `required`, `default`, or `min/max`. You’d use it in Laravel migrations by calling `$filter->getDbField()` and passing the result to `Schema::raw()`. However, this is currently MySQL-specific and requires manual adjustments for PostgreSQL or SQLite.
- How do I install and register alhames/filter-bundle in a Laravel project?
- Install via Composer: `composer require alhames/filter-bundle`. Register the bundle in `config/app.php` under `providers` as a Laravel `ServiceProvider`, then publish its config (if available) with `php artisan vendor:publish`. Note: The package is Symfony-native, so you’ll need to wrap its services in Laravel’s container or rewrite core logic for full compatibility.
- Does this package support Laravel’s Eloquent model casting or validation rules?
- No, the package focuses solely on SQL column generation. For Eloquent casting, you’d need to manually define `$casts` in your model. For validation, pair it with Laravel’s `FormRequest` or `Validator` to enforce rules like `boolean`, `numeric`, or custom logic. There’s no built-in integration with Laravel’s validation system.
- Will this work with Laravel’s query builder for dynamic WHERE clauses?
- Not out of the box. The package generates schema definitions but doesn’t interact with Laravel’s query builder. To add dynamic filtering, you’d need to extend it with a custom `FilterQueryBuilder` trait or manually build `where` clauses based on the generated column types. This would require additional development effort.
- Is alhames/filter-bundle compatible with Laravel 10+ and newer versions?
- The package itself doesn’t explicitly declare Laravel support, but its core functionality (SQL generation) is language-agnostic. However, due to its Symfony dependency and lack of Laravel-specific testing, you’ll need to verify compatibility manually. Start with a fresh Laravel project and test migrations/validation before integrating into production.
- How do I customize the SQL output for PostgreSQL or SQLite if I’m not using MySQL?
- Override the `getDbField()` method for each filter type (e.g., `BooleanFilter`, `FloatFilter`) to return database-specific syntax. For example, replace `TINYINT(1)` with `BOOLEAN` for PostgreSQL. Alternatively, use Laravel’s `DB::connection()->getDoctrineSchemaManager()` to conditionally generate schema based on the active database driver.
- Are there performance concerns with runtime SQL generation during migrations?
- Runtime generation adds minimal overhead for small schemas, but large migrations with hundreds of columns could slow down deployment. Test performance in staging by timing `php artisan migrate`. If critical, pre-generate SQL in a service class or cache the results during development.
- What alternatives exist for dynamic schema generation in Laravel without Symfony dependencies?
- Consider `spatie/laravel-model-states` for state-based column toggling or custom traits to encapsulate schema logic. For validation-heavy filters, Laravel’s built-in `FormRequest` or `Validator` with dynamic rules may suffice. If you need both schema *and* validation, a lightweight package like `laravel-attributes` (for PHP 8+) could offer a more Laravel-native solution.
- How do I handle NULL values or default values in dynamically generated columns?
- Pass `default` or `nullable` configurations to the filter classes (e.g., `new BooleanFilter(['default' => false])`). The package will include these in the generated SQL (e.g., `DEFAULT 0` for booleans). For NULL handling, ensure your filter logic accounts for `NULL` checks in application code, as the package doesn’t enforce this at the validation layer.
- Is there community support or examples for Laravel integration, or should I fork this package?
- The package has minimal Laravel-specific documentation or examples, and its Symfony origins mean most community support focuses on Symfony. If you’re committed to Laravel, fork the repo and adapt it—replace Symfony services with Laravel facades, add Laravel-specific tests, and document the changes. Contributing back to the original repo could benefit others if you generalize the fixes.