- Can I use nette/database in Laravel to replace Eloquent for raw SQL queries or reporting?
- Yes, nette/database is ideal for raw SQL, analytics, or bulk operations where Eloquent’s ORM feels limiting. It offers a fluent, PDO-like API with advanced features like joins, subqueries, and native drivers for better performance. However, it lacks Eloquent’s model layer, so you’d need to integrate it alongside Eloquent for full CRUD workflows.
- Does nette/database work with Laravel’s Query Builder syntax (e.g., DB::table()->where())?
- No, nette/database uses a different fluent syntax (e.g., Selection::from('users')->where('id = ?', $id)). While the concepts are similar, you’ll need to rewrite queries or create adapter layers. Laravel’s Query Builder and nette/database can coexist but aren’t directly interchangeable.
- Will nette/database break Laravel’s migrations or seeders?
- No, nette/database won’t interfere with Laravel’s migrations or seeders. Its schema reflection tools (e.g., exploring tables/columns) can even *reduce* boilerplate in migrations by auto-generating `down()` methods. Just ensure you’re not mixing PDO and native drivers in the same connection.
- How do I install nette/database in a Laravel project without conflicts?
- Install via Composer: `composer require nette/database`. To avoid conflicts, configure it as a separate connection in `config/database.php` (e.g., `nette` driver) and use it via `DB::connection('nette')->select(...)` or a custom facade. Avoid replacing Laravel’s default PDO connection unless you’re ready to refactor all queries.
- Does nette/database support transactions in Laravel, and how does it compare to DB::transaction()?
- Yes, nette/database supports transactions (since v3.1.0) with `Connection::beginTransaction()`. It offers nested transactions (v3.1.2+) and finer-grained control than Laravel’s `DB::transaction()`, but you’ll need to manually handle rollbacks. For simplicity, stick to Laravel’s transaction helpers unless you need advanced features like savepoints.
- Can I use nette/database with Laravel’s Eloquent models for hybrid queries?
- Not natively, but you can combine them by using nette/database for raw SQL in model methods (e.g., `$user->customQuery()`) or via query scopes. For example, fetch a model’s related data with `Selection::from('posts')->where('user_id', $user->id)` and hydrate it manually. This requires careful mapping between nette’s `ActiveRow` and Eloquent’s `Collection`.
- What Laravel versions and PHP requirements does nette/database support?
- nette/database v3.x requires PHP 8.1+, which aligns with Laravel 9/10. For Laravel 8 (PHP 8.0), use v2.x, but note it lacks newer features like nested transactions. If you’re on PHP 7.4 (Laravel 7/8), this package isn’t compatible. Always check the [release notes](https://github.com/nette/database/releases) for version-specific changes.
- Are there performance benefits over Laravel’s Query Builder or PDO?
- Yes, nette/database’s native drivers (MySQL, PostgreSQL, etc.) often outperform PDO by reducing abstraction layers. Benchmarks show faster execution for complex queries, especially with bulk operations or joins. However, test in your environment—some queries may behave differently due to syntax changes (e.g., `where('id = ?')` vs. `whereId($id)`).
- How does nette/database handle caching compared to Laravel’s cache?
- nette/database doesn’t include built-in caching, but you can integrate Laravel’s cache (e.g., `Cache::remember()`) with its `Selection::refreshData()` or `ResultSet` objects. For example, cache query results: `$cached = Cache::get('users'); return $cached ?? Selection::from('users')->fetchAll();`. This requires manual setup but leverages Laravel’s robust caching.
- What alternatives to nette/database exist for Laravel’s query builder?
- For Laravel, consider sticking with the built-in Query Builder or Eloquent for ORM needs. Other alternatives include **Doctrine DBAL** (enterprise-grade, complex setup), **Cycle ORM** (for advanced data mapping), or **Laravel’s own Query Builder extensions** (e.g., `whereBetween`, `orWhere`). If you need a fluent API like nette/database, **Cycle’s QueryBuilder** or **ParagonIE’s SQL Builder** are lighter options, though they lack nette’s schema reflection.