- How do I connect Laravel Eloquent to Google Cloud Spanner?
- You’ll need to create a custom Eloquent connection driver. Use the `DB::extend()` method to register a Spanner connection, then wrap the `SpannerClient` in a class implementing Laravel’s `ConnectionInterface`. Example: `DB::extend('spanner', function($config) { return new SpannerConnection(new SpannerClient(), $config); });`. This requires mapping Spanner’s SQL dialect to Eloquent’s query builder.
- What Laravel versions are supported by this package?
- The package itself is framework-agnostic, but Laravel compatibility depends on your custom integration layer. Test with Laravel 8.x–10.x for Eloquent/Query Builder support. For raw queries, ensure your PHP version (8.1+) aligns with the package’s requirements. Check the [Google Cloud PHP docs](https://cloud.google.com/php/docs) for PHP version constraints.
- How do I handle authentication in Laravel’s `.env` file?
- Store your Google Cloud service account key path in `.env` as `GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json`. The package auto-detects credentials via the `google/auth` library. For production, use Workload Identity Federation or ADC (Application Default Credentials) to avoid hardcoding secrets. Never commit `.env` to version control.
- Will this work in a Laravel Forge or shared hosting environment?
- No. The package requires the PHP gRPC extension, which isn’t available on shared hosting. Forge/VPS setups need custom PHP builds with `pecl install grpc`. Docker is recommended for local/testing environments. Check [Google’s gRPC guide](https://cloud.google.com/php/grpc) for installation steps.
- Can I use Spanner’s multiplexed sessions for high-traffic Laravel apps?
- Yes, multiplexed sessions (V2) are ideal for high concurrency. Enable them by default in the `SpannerClient` constructor. For Laravel, configure connection pooling in your custom driver to reuse sessions. Monitor session counts in Cloud Spanner metrics to avoid hitting limits (default: 100 concurrent sessions per instance).
- How do I migrate an existing Laravel MySQL/PostgreSQL app to Spanner?
- Start by rewriting schema migrations to use Spanner’s DDL (e.g., `ALTER TABLE`). Replace MySQL-specific features (like triggers) with application logic or Spanner’s computed columns. Use a pilot module (e.g., orders) to test queries, then gradually migrate tables. Tools like `spanner-migrate` can help convert SQL dialects. Budget for data load times (TB-scale datasets may take hours).
- Are there Laravel-specific optimizations for Spanner queries?
- No built-in Laravel optimizations exist, but you can cache frequent queries with PSR-6 (Spanner supports caching via middleware). Use Spanner’s `EXECUTE_SQL` with parameterized queries to avoid N+1 issues. For analytics, leverage Spanner’s interleaved tables instead of Laravel’s eager loading. Monitor query patterns in Cloud Logging to identify slow queries.
- What’s the cost difference between Spanner and Cloud SQL PostgreSQL for Laravel?
- Spanner is significantly more expensive due to node-hour pricing (starts at ~$0.90/node-hour for regional instances). Cloud SQL PostgreSQL charges per query (~$0.0005/10K queries) and is cheaper for read-heavy workloads. Use Spanner only if you need global consistency; otherwise, Cloud SQL or even self-hosted PostgreSQL may suffice for Laravel.
- How do I debug Spanner connection issues in Laravel?
- Enable gRPC logging by setting `GRPC_VERBOSITY=DEBUG` in your environment. Use Spanner’s request IDs for tracing in Cloud Logging. For Laravel, wrap the `SpannerClient` in a middleware to log errors and query execution times. Check GCP’s [Spanner troubleshooting guide](https://cloud.google.com/spanner/docs/troubleshooting) for common issues like throttling or schema errors.
- Are there alternatives to this package for Laravel + Google Cloud?
- For relational data, consider `google/cloud-sql` (PostgreSQL/MySQL) or `laravel-spanner` (community packages). For NoSQL, use `google/cloud-firestore`. If you need a managed PostgreSQL with Laravel compatibility, Cloud SQL PostgreSQL is a drop-in replacement. Spanner is unique for global consistency but lacks features like PostgreSQL’s full-text search or extensions.