PHP code example of nomansheikh / laravel-bigquery-eloquent
1. Go to this page and download the library: Download nomansheikh/laravel-bigquery-eloquent library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nomansheikh / laravel-bigquery-eloquent example snippets
'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', ''),
],
],
namespace App\Models;
use NomanSheikh\LaravelBigqueryEloquent\Eloquent\BigQueryModel;
class UserAnalytics extends BigQueryModel
{
protected $table = 'user_analytics'; // Automatically prefixed with project.dataset
}
namespace App\Models;
use NomanSheikh\LaravelBigqueryEloquent\Eloquent\BigQueryModel;
class SalesData extends BigQueryModel
{
protected $table = 'sales';
// Override dataset statically
protected ?string $dataset = 'analytics';
// Or override dynamically in constructor
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setDataset('sales_data');
}
}
// Basic query
$users = UserAnalytics::where('page_views', '>', 100)->get();
// Complex query with ordering and limits
$topUsers = UserAnalytics::select('user_id', 'page_views')
->where('created_at', '>=', now()->subDays(30))
->orderBy('page_views', 'desc')
->limit(10)
->get();
// Aggregations
$stats = UserAnalytics::selectRaw('
COUNT(*) as total_users,
AVG(page_views) as avg_page_views,
SUM(session_duration) as total_duration
')->first();
// Using whereRaw with JSON_EXTRACT_SCALAR
$results = UserAnalytics::whereRaw("JSON_EXTRACT_SCALAR(json_column, '$.key') = ?", ['value'])->get();
// Using selectRaw for JSON extraction
$results = UserAnalytics::selectRaw("JSON_EXTRACT_SCALAR(json_column, '$.key') as key_value")->get();
use Illuminate\Support\Facades\DB;
$results = DB::connection('bigquery')->select(
'SELECT user_id, COUNT(*) as visits FROM `project.dataset.user_analytics` WHERE created_at >= ?',
[now()->subDays(7)]
);