google/cloud-bigquery
Idiomatic PHP client for Google BigQuery. Create and manage datasets/tables, load data (e.g., CSV), run query jobs, and iterate results. Part of Google Cloud PHP; includes auth, debugging guidance, and full API docs.
bind() in AppServiceProvider) to interact with BigQuery without tightly coupling business logic to the database layer.LoadBigQueryDataJob to ingest CSV files asynchronously). Works seamlessly with Laravel Horizon for monitoring long-running jobs (e.g., BigQuery load operations).runQuery(), load()) mirror Laravel’s Http facade, reducing learning curves for backend teams.// In a Laravel controller:
$results = app(BigQueryClient::class)->query('SELECT * FROM user_metrics');
return view('dashboard', ['metrics' => $results]);
GOOGLE_APPLICATION_CREDENTIALS). Example:
$bigQuery = new BigQueryClient([
'keyFilePath' => env('GOOGLE_CREDENTIALS_PATH'),
]);
.env for dynamic credentials/project IDs:
BIGQUERY_PROJECT_ID=my-project
BIGQUERY_DATASET=analytics
$cacheKey = 'user_metrics_' . $userId;
return Cache::remember($cacheKey, now()->addHours(1), function () use ($bigQuery) {
return $bigQuery->query("SELECT * FROM user_metrics WHERE user_id = {$userId}");
});
// Write to PostgreSQL (Eloquent)
User::create([...]);
// Read from BigQuery (for analytics)
$bigQuery->query("SELECT COUNT(*) FROM users WHERE created_at > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)");
$mock = Mockery::mock(BigQueryClient::class);
$mock->shouldReceive('query')->andReturn([...]);
$this->app->instance(BigQueryClient::class, $mock);
| Risk | Mitigation Strategy | Severity |
|---|---|---|
| Authentication Complexity | Use Laravel’s .env + GCP Service Accounts. Document a BigQueryServiceProvider for DI. |
Low |
| Query Performance | Implement Laravel middleware to validate query complexity (e.g., MAX_EXECUTION_TIME). |
Medium |
| Cost Overruns | Set up BigQuery slot reservations and Laravel middleware to log/query costs. | High |
| Schema Mismatches | Use Laravel migrations + BigQuery’s INFORMATION_SCHEMA to validate schemas. |
Medium |
| Dependency Bloat | Scope the package to only analytics-heavy features (e.g., avoid using it for CRUD). | Low |
| PHP Version Support | Pin to PHP 8.1+ (Laravel’s LTS) and test against Laravel 10/11. | Low |
| Vendor Lock-in | Abstract BigQuery logic behind a repository pattern (e.g., BigQueryRepository) for future swaps. |
Medium |
Data Flow:
load()), or is it a read-only analytics layer?Performance:
Cost:
Security:
authorize() middleware.)Team Skills:
Alternatives:
Laravel Core:
BigQueryClient in AppServiceProvider:
$this->app->singleton(BigQueryClient::class, function ($app) {
return new BigQueryClient([
'projectId' => env('BIGQUERY_PROJECT_ID'),
'keyFilePath' => env('GOOGLE_CREDENTIALS_PATH'),
]);
});
BigQuery facade for cleaner syntax:
use Illuminate\Support\Facades\Facade;
class BigQuery extends Facade {
protected static function getFacadeAccessor() { return 'bigquery'; }
}
Usage:
$results = BigQuery::query('SELECT * FROM users');
BigQueryJobCompleted):
event(new BigQueryJobCompleted($job));
Database Layer:
// Write to PostgreSQL (Eloquent)
User::create([...]);
// Read from BigQuery (analytics)
$userMetrics = BigQuery::query("SELECT * FROM user_metrics WHERE user_id = {$user->id}");
public function up() {
$table = $this->bigQuery->dataset('analytics')->table('user_metrics');
$table->create([
'schema' => [
['name' => 'user_id', 'type' => 'STRING'],
['name' => 'metric', 'type' => 'FLOAT'],
],
]);
}
Queue System:
// Dispatch a job to load data into BigQuery
LoadBigQueryData::dispatch($filePath, 'my_dataset.my_table');
// Job class
class LoadBigQueryData implements ShouldQueue {
public function handle() {
$table = $this->bigQuery->dataset('my_dataset')->table('my_table');
$job = $table->load(fopen($this->filePath, 'r'));
$job->waitForCompletion();
}
}
API Layer:
GET /api/metrics):
public function showMetrics() {
$results = BigQuery::query("SELECT * FROM user_metrics WHERE date = CURRENT_DATE()");
return response()->json($results);
}
.env.BigQueryServiceProvider to bind the client.DB::select() calls with `BigQuery::queryHow can I help you explore Laravel packages today?