- How do I install and configure this package for Laravel?
- Run `composer require ntanduy/cloudflare-d1-database`, then add the D1 connection to your `.env` with `D1_DATABASE_ID` and `D1_ACCOUNT_ID`. Configure the connection in `config/database.php` under `connections.d1`. For Worker mode, also set `D1_WORKER_URL` and `D1_WORKER_HMAC_SECRET`.
- Does this package support Laravel Eloquent models and migrations?
- Yes, it fully supports Eloquent ORM and Query Builder. Migrations work, but rollbacks are unsupported due to D1’s immutable design. Use `php artisan d1:schema-dump` to export your schema for version control.
- What Laravel versions are supported, and is PHP 8.1 compatible?
- This package supports Laravel 10–13.x and requires PHP 8.2+. PHP 8.1 or lower is not supported due to dependency requirements.
- How do I choose between REST and Worker drivers?
- Use the REST driver for simplicity in development or low-throughput apps. For production with low-latency needs, deploy the Worker driver (requires a Cloudflare Worker). Worker mode also enables read replicas and auto read/write splitting.
- Can I use D1 for complex transactions or multi-statement queries?
- D1 lacks traditional ACID transactions. Use batch queries for atomicity, but avoid multi-statement transactions. For critical operations, consider a hybrid approach (e.g., D1 for reads, PostgreSQL for writes).
- How do I handle D1’s SQL limitations, like no LIMIT in UPDATE statements?
- Workarounds include using Common Table Expressions (CTEs) with `WITH` clauses or restructuring queries. The package documentation provides examples for common limitations. Test edge cases early in your migration.
- Is there a way to mock D1 interactions in PHPUnit tests?
- Yes, the package uses Saloon HTTP clients, which can be mocked with PHPUnit. For D1-specific behavior (e.g., batch queries), create custom test doubles or use the `DatabaseMocker` trait provided in the package.
- How do I import an existing database schema into D1?
- Use the `php artisan d1:import` command to import SQL files. For large datasets, consider breaking the import into smaller batches or using the `d1:time-travel` command to restore from a dump.
- What are the costs associated with using Cloudflare D1?
- D1 offers a free tier with 1GB storage and 100MB/day compute. Monitor usage in staging to avoid exceeding limits. Production workloads may incur costs based on storage and compute usage.
- Are there alternatives if I need full transaction support?
- If transactions are critical, consider using a traditional database like PostgreSQL or MySQL alongside D1. For read-heavy workloads, D1 can serve as a global cache layer while PostgreSQL handles writes.