Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Cloud Spanner

Cloud Spanner Laravel Package

google/cloud-spanner

Idiomatic PHP client for Google Cloud Spanner, a globally consistent relational database. Install via Composer and use gRPC to connect to instances/databases, run SQL queries with parameters, and benefit from V2 multiplexed sessions for efficient concurrent requests.

View on GitHub
Deep Wiki
Context7

Cloud Spanner Client for PHP

Frequently asked questions about Cloud Spanner
How do I integrate Google Cloud Spanner with Laravel’s Eloquent ORM?
You’ll need a custom connection driver (e.g., `SpannerConnection`) to bridge Laravel’s query builder with Spanner’s SQL dialect. Start by extending Laravel’s `Connection` class and overriding methods like `select()`, `insert()`, and `update()` to translate queries to Spanner-compatible SQL. For relationships, adjust Eloquent models to handle Spanner’s interleaved tables or foreign key constraints manually. The package itself doesn’t provide Eloquent integration out of the box, so you’ll need to build this layer yourself or use a community package like `laravel-spanner`.
What Laravel versions are supported by this package?
The `google/cloud-spanner` package doesn’t enforce Laravel version constraints directly, but it requires **PHP 8.2+** due to gRPC and Spanner v2 features like multiplexed sessions. For Laravel compatibility, ensure your app uses **Laravel 10.x or 11.x** (which support PHP 8.2+). Test thoroughly, as some Laravel features like schema migrations or query builder syntax may need adjustments for Spanner’s SQL dialect. Always check the package’s [PHP requirements](https://github.com/googleapis/google-cloud-php-spanner) for updates.
How do I handle authentication in Laravel for Google Cloud Spanner?
Use Google Cloud’s Application Default Credentials (ADC) or a service account JSON key file. Store the path to the key file in Laravel’s `.env` (e.g., `SPANNER_CREDENTIALS_PATH=./path/to/key.json`) and load it in your `AppServiceProvider` or a custom service provider. Rotate credentials securely via CI/CD pipelines or a secrets manager like HashiCorp Vault. The package’s `SpannerClient` will automatically use these credentials for authentication. Avoid hardcoding keys in your repository.
Can I use Spanner’s multiplexed sessions (v2) in Laravel for high concurrency?
Yes, multiplexed sessions (enabled by default in v2) reduce backend resource usage by managing concurrent requests efficiently. In Laravel, configure the `SpannerClient` with a PSR-6 compatible cache (e.g., `FilesystemAdapter` or `RedisAdapter`) for session caching. Register the client as a singleton in `AppServiceProvider` and reuse it across requests. This is ideal for Laravel APIs with bursty traffic, but monitor session cache performance under load, especially in multi-region deployments.
How do I handle Laravel migrations with Spanner’s lack of ALTER TABLE support?
Spanner requires DDL statements (e.g., `CREATE TABLE IF NOT EXISTS`) instead of ALTER TABLE. In Laravel, replace `Schema::table()` migrations with raw SQL queries or custom migration classes that execute DDL statements. For example, use `$db->execute('CREATE TABLE users (...)')` in a migration file. Avoid Laravel’s schema builder entirely for Spanner. Document your schema changes carefully, as Spanner doesn’t support traditional migration rollbacks for structural changes.
What are the performance implications of using Spanner with Laravel in production?
Spanner excels at global-scale OLTP workloads with strong consistency, but latency can vary by region. Use **routing hints** (e.g., `read_only` transactions) to optimize for low-latency access. For Laravel, cache frequent queries at the application level (e.g., Redis) and leverage Spanner’s interleaved tables to reduce join overhead. Monitor query performance with Cloud Spanner’s built-in metrics and adjust indexes or partitioning as needed. Avoid N+1 queries by using Eloquent’s `with()` or raw SQL with JOINs.
How do I implement soft deletes in Laravel with Google Cloud Spanner?
Spanner supports soft deletes via `DELETE WHERE` clauses, but Laravel’s `SoftDeletes` trait won’t work out of the box. Create a custom scope in your Eloquent model, like `scopeSoftDeleted($query)`, that filters for records where `deleted_at IS NULL`. Override the `delete()` method to update `deleted_at` instead of deleting rows. For bulk operations, use raw SQL or a custom query builder method. Example: `$user->update(['deleted_at' => now()])` instead of `$user->delete()`.
Is there a fallback option if Spanner’s global consistency isn’t required?
Yes, consider **Google Cloud SQL for PostgreSQL** or **CockroachDB** for distributed SQL with lower latency. For Laravel, use the `pgsql` or `pdo_pgsql` drivers with a connection pooler like PgBouncer. If you need eventual consistency, **Firestore** (NoSQL) or **Bigtable** (wide-column) could also fit, though they require rewriting Eloquent models. Evaluate your consistency requirements: Spanner is overkill for single-region apps, but Cloud SQL or CockroachDB offer a middle ground with easier Laravel integration.
How do I monitor Spanner’s latency and session health in a Laravel app?
Use **Google Cloud’s Operations Suite (formerly Stackdriver)** to monitor Spanner’s latency, CPU usage, and session health. Integrate Cloud Logging and Cloud Monitoring with Laravel via the `google/cloud-logging` and `google/cloud-monitoring` PHP clients. Log custom metrics in Laravel’s `AppServiceProvider` or middleware, such as query execution times or session creation failures. Set up alerts for high latency or failed sessions. For production, ensure your Laravel app forwards logs to Cloud Logging using a stack driver handler.
What’s the best way to deploy Laravel with Spanner in a multi-region setup?
Deploy Laravel in a region closest to your primary Spanner instance to minimize latency. Use **Google Cloud Load Balancing** to route traffic to Laravel instances across regions for high availability. Configure Spanner’s **read-only replicas** in secondary regions for analytics or failover. In Laravel, use a connection pooler like **PgBouncer** (for PostgreSQL fallback) or manage Spanner sessions via multiplexed sessions (v2) to reduce overhead. Test failover scenarios by simulating region outages and ensure Laravel’s queue workers or cron jobs can reconnect to Spanner automatically.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai