PHPackages                             nomansheikh/laravel-bigquery-eloquent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. nomansheikh/laravel-bigquery-eloquent

ActiveLibrary[Database &amp; ORM](/categories/database)

nomansheikh/laravel-bigquery-eloquent
=====================================

Query Google BigQuery tables with Laravel Eloquent.

v1.3.2(8mo ago)4444↓46.7%2[1 issues](https://github.com/nomansheikh/laravel-bigquery-eloquent/issues)[4 PRs](https://github.com/nomansheikh/laravel-bigquery-eloquent/pulls)MITPHPPHP ^8.3CI passing

Since Aug 16Pushed 4mo agoCompare

[ Source](https://github.com/nomansheikh/laravel-bigquery-eloquent)[ Packagist](https://packagist.org/packages/nomansheikh/laravel-bigquery-eloquent)[ Docs](https://github.com/nomansheikh/laravel-bigquery-eloquent)[ GitHub Sponsors]()[ RSS](/packages/nomansheikh-laravel-bigquery-eloquent/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (13)Versions (12)Used By (0)

Laravel BigQuery Eloquent
=========================

[](#laravel-bigquery-eloquent)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5bb73b9ba77766803962a77c3b10c93b9d1a29f2fec15374588ed1d889f44e69/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6f6d616e736865696b682f6c61726176656c2d62696771756572792d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nomansheikh/laravel-bigquery-eloquent)
[![GitHub Tests Action Status](https://camo.githubusercontent.com/02dd91a335615829235670dd82a3cdce6d3eb770d26668a49312928e32c46c80/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e6f6d616e736865696b682f6c61726176656c2d62696771756572792d656c6f7175656e742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/nomansheikh/laravel-bigquery-eloquent/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://camo.githubusercontent.com/ddbeb02bbbf2995d059f367b45092070faeee98316021c3e89f630b677d43ac2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e6f6d616e736865696b682f6c61726176656c2d62696771756572792d656c6f7175656e742f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/nomansheikh/laravel-bigquery-eloquent/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)
[![Total Downloads](https://camo.githubusercontent.com/fbe31b28568b5234001e1e142aea4ae6ada215bf9fd0a178625f71b015838139/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e6f6d616e736865696b682f6c61726176656c2d62696771756572792d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nomansheikh/laravel-bigquery-eloquent)

---

Overview
--------

[](#overview)

**Laravel BigQuery Eloquent** is a Laravel package that seamlessly integrates Google BigQuery with Laravel's Eloquent ORM. It enables you to query BigQuery tables using familiar Eloquent syntax, simplifying analytics and data querying directly within your Laravel applications.

---

Features
--------

[](#features)

- **Eloquent Integration**: Use BigQuery tables as Eloquent models.
- **Dedicated BigQuery Driver**: Optimized database driver for BigQuery.
- **Automatic Fully Qualified Table Names**: Handles `project.dataset.table` formatting transparently.
- **Custom Query Grammar**: Generates SQL optimized for BigQuery syntax.
- **Read-Only Support**: Supports SELECT queries (read operations only).
- **Flexible Authentication**: Supports Application Default Credentials (ADC) and service account key files.
- **Environment Configuration**: Easy setup via environment variables.

---

Requirements
------------

[](#requirements)

- PHP 8.3 or higher
- Laravel 10.x, 11.x, or 12.x
- Access to Google Cloud BigQuery API
- Google Cloud authentication (Application Default Credentials recommended)

---

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require nomansheikh/laravel-bigquery-eloquent
```

---

Configuration
-------------

[](#configuration)

### 1. Publish the configuration file

[](#1-publish-the-configuration-file)

Run the following Artisan command to publish the package config:

```
php artisan vendor:publish --provider="NomanSheikh\LaravelBigqueryEloquent\LaravelBigqueryEloquentServiceProvider"
```

### 2. Authentication Setup

[](#2-authentication-setup)

The package supports Google Cloud's recommended authentication hierarchy:

- **Recommended: Application Default Credentials (ADC)**

    - Local development: Run `gcloud auth application-default login`
    - Production: Use a service account attached to your compute instance or set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
- **Alternative: Service Account Key File**

    - Download a JSON key file from Google Cloud Console.
    - Set the `BIGQUERY_KEY_FILE` environment variable pointing to the JSON file (not recommended for production).

#### Authentication Hierarchy

[](#authentication-hierarchy)

The Google Client library authenticates in this order:

1. `key_file` specified in the database config.
2. `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
3. Default credential file locations.
4. Google App Engine built-in service account.
5. Google Compute Engine built-in service account.
6. Direct credentials array in config.

Example direct credentials array in `config/database.php`:

```
'bigquery' => [
    'driver'     => 'bigquery',
    'project_id' => env('BIGQUERY_PROJECT_ID', ''),
    'dataset'    => env('BIGQUERY_DATASET', ''),
    'key_file'   => [
        'type' => env('GOOGLE_CLOUD_ACCOUNT_TYPE'),
        'private_key_id' => env('GOOGLE_CLOUD_PRIVATE_KEY_ID'),
        'private_key' => env('GOOGLE_CLOUD_PRIVATE_KEY'),
        'client_email' => env('GOOGLE_CLOUD_CLIENT_EMAIL'),
        'client_id' => env('GOOGLE_CLOUD_CLIENT_ID'),
        'auth_uri' => env('GOOGLE_CLOUD_AUTH_URI'),
        'token_uri' => env('GOOGLE_CLOUD_TOKEN_URI'),
        'auth_provider_x509_cert_url' => env('GOOGLE_CLOUD_AUTH_PROVIDER_CERT_URL'),
        'client_x509_cert_url' => env('GOOGLE_CLOUD_CLIENT_CERT_URL'),
    ],
],
```

### 3. Environment Variables

[](#3-environment-variables)

Add the following to your `.env` file:

```
BIGQUERY_PROJECT_ID=your-project-id
BIGQUERY_DATASET=your-dataset-name
# Optional: Only if using service account key file (not recommended for production)
# BIGQUERY_KEY_FILE=path/to/your/service-account-key.json
```

### 4. Database Connection

[](#4-database-connection)

Add the BigQuery connection in `config/database.php`:

```
'connections' => [
    // ... other connections ...

    'bigquery' => [
        'driver'     => 'bigquery',
        'project_id' => env('BIGQUERY_PROJECT_ID', ''),
        'dataset'    => env('BIGQUERY_DATASET', ''),
        // Optional: Only if using service account key file (not recommended)
        'key_file'   => env('BIGQUERY_KEY_FILE', ''),
    ],
],
```

---

Usage
-----

[](#usage)

### Models

[](#models)

Create models by extending `BigQueryModel` to interact with BigQuery tables:

```
