- How do I install Raw Hydrator in a Laravel project?
- Run `composer require minionfactory/raw-hydrator` and register the service provider in `config/app.php` under the `providers` array. No additional configuration is needed for basic usage, though you may extend it via custom mappings.
- Can Raw Hydrator handle nested relations (e.g., `Post->author->comments`) in a single query?
- Yes, but your raw SQL must explicitly join and select all required columns for nested relations. The package then hydrates them automatically. For deep relations (>2 levels), test thoroughly—circular references or missing columns may cause failures.
- Does Raw Hydrator work with Laravel 9/10 and PHP 8.0+?
- Yes, the package explicitly requires PHP 8.0+ and is compatible with Laravel 9 and 10. It leverages Eloquent’s hydration system, so no additional Laravel version-specific configurations are needed.
- How does Raw Hydrator compare to using `DB::select()` with manual model instantiation?
- Raw Hydrator automates relation hydration, reducing boilerplate code like `new Model($row)`. It’s faster for complex queries (e.g., CTEs, window functions) where Eloquent’s query builder lacks flexibility, but manual hydration gives you more control over edge cases.
- What’s the best way to debug hydration failures or mismatched relations?
- Enable query logging in Laravel (`'debug' => true` in `config/database.php`) to inspect raw SQL. Use `dd($hydrator->getResults())` to verify the structure of returned data before hydration. For relation issues, check column aliases in your SQL against model attributes.
- Can I use Raw Hydrator for write operations (e.g., bulk inserts/updates)?
- No, this package is designed for read operations only. For write operations, use Eloquent’s built-in methods like `create()`, `update()`, or query builder. Hydration occurs post-query, so it’s not suitable for mutations.
- How does Raw Hydrator handle large datasets (e.g., 50K+ rows)?
- Performance degrades with very large datasets due to memory usage. For datasets >10K rows, consider chunking results or using cursors. Benchmark with `memory_get_usage()` and `microtime()` to assess trade-offs against alternatives like `with()` or manual hydration.
- Are there any known issues with circular relations or polymorphic models?
- Yes, circular relations (e.g., `User->posts->author->posts`) or polymorphic relations may fail silently. The package assumes a static schema, so dynamic or polymorphic setups require custom mapping logic in `Hydrator::map()`. Test edge cases thoroughly.
- Can I customize how columns map to model attributes or relations?
- Yes, override the `Hydrator::map()` method to define custom column-to-attribute mappings. For example, rename columns or handle snake_case/camelCase conversions. You can also extend `Hydrator::hydrate()` for post-processing logic like computed attributes.
- What are the alternatives to Raw Hydrator for raw SQL hydration in Laravel?
- Alternatives include manually hydrating models with `DB::select()` or using Eloquent’s `with()` for relations (though it may cause N+1 queries). For advanced use cases, consider packages like `spatie/laravel-query-builder` or `stancl/tenancy` for multi-tenant hydration. Raw Hydrator excels in performance for read-heavy, relation-dense queries.