- How do I install DoctrineJsonFunctions in a Laravel project?
- Run `composer require scienta/doctrine-json-functions` and register the custom functions in your Doctrine configuration. For Laravel, add the functions to `config/doctrine.yaml` under `orm.entity_managers.default.dql.custom_string_functions` or use a service provider to centralize the setup.
- Does this package work with Laravel Eloquent?
- Yes, it integrates directly with Eloquent’s QueryBuilder. Use JSON functions like `whereJsonContains()` or `whereJsonExtract()` in your Eloquent queries, and the package translates them to DQL. Example: `User::whereJsonContains('metadata', '"premium"')->get()`.
- Which Laravel versions and Doctrine ORM versions are supported?
- The package supports Laravel 8+ and Doctrine ORM 2.x. Check the [GitHub repository](https://github.com/ScientaNL/DoctrineJsonFunctions) for the latest compatibility matrix, as it may evolve with minor updates. Always test in a staging environment before production deployment.
- Can I use this package with multiple databases (e.g., MySQL and PostgreSQL)?
- Yes, the package supports MySQL, PostgreSQL, SQLite, and SQL Server. It validates platform compatibility at runtime, so using a PostgreSQL function on a MySQL connection will throw an early error. Laravel’s connection switching (e.g., `connection('pgsql')`) works seamlessly with this package.
- What if my database version doesn’t support all JSON functions?
- Some functions (e.g., `JSON_VALUE` in MySQL 8.0.21+) require specific database versions. The package will fail gracefully if unsupported functions are used. Validate your database version during deployment (e.g., via migrations or CI checks) to avoid runtime errors.
- How do I handle boolean JSON functions like `JSON_CONTAINS` in Eloquent?
- Boolean functions return 1 or 0, so you must compare them explicitly in DQL (e.g., `JSON_CONTAINS(column, value) = 1`). For cleaner code, create a Laravel repository method or Eloquent macro like `whereJsonContains()` that abstracts this comparison.
- Will this package slow down my queries if I use JSON functions heavily?
- JSON functions can be resource-intensive, especially on large datasets. Index JSON paths (e.g., PostgreSQL’s `jsonb_path_ops`) and benchmark queries in staging. For complex queries, consider materialized views or application-side caching to mitigate performance overhead.
- Are there alternatives to this package for Laravel JSON queries?
- Yes, alternatives include raw SQL queries (e.g., `DB::select()`) or packages like `spatie/laravel-query-builder` for custom query logic. However, this package provides a DQL-native solution, reducing raw SQL dependency and improving portability across databases.
- How can I test this package in a Laravel project with multiple databases?
- Use Laravel’s database testing utilities to switch connections (e.g., `DatabaseMigrations` or `DatabaseTransactions`). Write integration tests for each supported database, mocking the Doctrine configuration to validate function behavior. The package’s integration tests cover multi-database scenarios.
- Is there a Laravel-specific wrapper for this package to simplify usage?
- Currently, no official Laravel wrapper exists, but you can create one. For example, build a trait or helper class to wrap Eloquent queries (e.g., `JsonQueryHelper::whereJsonExtract()`). This abstracts Doctrine configuration and improves readability for your team.