Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use laravel 5 query builder for google big query data extraction?

I am working on Google big queries and I wanted to know that either I can use laravel query builder to get data from google big query service?

like image 563
Hassan Raza Avatar asked Oct 17 '25 13:10

Hassan Raza


2 Answers

I was working on the same thing and trying to figure out the solution for that I user laravel 5 query builder and then I get an idea if we get the query from query builder and get bindings from query builder then we can easily use that query directory for the google bigquery Let me explain with the code below.

$users = DB::table('users')
        ->where('id','=','3');

Now i will use the function that will return me mysql query.

$sql = $users->toSql(); //this will return the sql query from query builder.

The output of above statement will be like this

select * from users where id = ?

Now I have another function that will give me the binding of that query.

$bindings = $users->getBindings(); //this statement will return me 3

After that if we want complete mysql query with binding then we have another function to do that.

public function getSqlWithBinding($sql,$bindDataArr){
    foreach($bindDataArr as $binding)
    {
        $value = is_numeric($binding) ? $binding : "'".$binding."'";
        $sql = preg_replace('/\?/', $value, $sql, 1);
    }
    return $sql;
}

Just pass both $sql = $users->toSql(); and $bindings = $users->getBindings(); in parameter to the above function and get the complete query like below.

select * from users where id = 3

Now that sql query you can use for you google bigquery without any confusion.

like image 75
Hassan Raza Avatar answered Oct 20 '25 19:10

Hassan Raza


You can't. The query builder is meant for database protocols. Currently, Laravel supports four databases:

MySQL
PostgreSQL
SQLite
SQL Server

BigQuery instead is provided as a service, and has it's own SDK. You need to add the to composer.json the BigQuery Cloud package.

like image 24
Pentium10 Avatar answered Oct 20 '25 18:10

Pentium10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!