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 fully managed relational database with global scale, strong transactional consistency, SQL support, and high availability. Install via Composer, authenticate, and run queries; supports gRPC and multiplexed sessions.

View on GitHub
Deep Wiki
Context7

google/cloud-spanner is an idiomatic PHP client for Google Cloud Spanner, a fully managed relational database offering global scale, strong transactional consistency, and high availability. It’s part of the broader Google Cloud PHP ecosystem and includes up-to-date API references and guidance for production use.

Designed for modern PHP apps, it supports gRPC transport and provides streamlined access to databases, SQL queries, and session management—including Multiplexed Sessions to handle high concurrency efficiently.

  • Composer installation and integration with Google Cloud PHP
  • Authentication support via Google Cloud credentials and guides
  • Execute SQL (ANSI 2011 + extensions) with parameter binding
  • Multiplexed Sessions for high-concurrency workloads
  • Optional session caching via PSR-6 compatible caches
Frequently asked questions about Cloud Spanner
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.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport