Google Cloud app engine with Laravel: unix_socket issue - laravel

I am using Laravel version 5.1.*. Here is my db config file...
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'unix_socket' => '/cloudsql/zoho-portal-159018:us-central1:zoho-portal',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
it gives error No such file or directory. It seems, unix_socket is the issue. But exact same config works when I use normal PDO connect without laravel....
$DBH_SOCKET = '/cloudsql/zoho-portal-159018:us-central1:zoho-portal';
$DBH_NAME = 'DBH_NAME';
$cns="mysql:unix_socket=".$DBH_SOCKET.";dbname=".$DBH_NAME.";charset=utf8";
$user='user';
$password='password';
try{
$DBH = new PDO($cns,$user,$password);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
If It works in normal single php file without laravel... then it must should work in laravel way. But not not working.. Any comments highly appreciated. Thanks

Related

LARAVEL Eloquent query multiple schema

Do i need to create multiple connection to access different database/schema. Cant I use with one dbconnection. Is there a way to pass the database name in the laravel eloquent or db builder? Currently in raw php i use one connection to query the different schema.
Create different connections to your database.php file and then pass them to your eloquent models.
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
// connection 2
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Suppose i have model User.php uses mysql connection named mysql1
inside my model i will add :
protected $connection = 'mysql1';
if i want to use mysql connection named mysql2
then i will use
protected $connection = 'mysql2';
Here i am setting connections statically in to models.
In Eloquent, I use DB:connection() to set my named connection, like so:
$query = DB::connection('db_connection_name')->table($this->table)
If you weren't aware, the database connections are named in the config app/config/database.php

Laravel homestead: Unknown database 'projectadmin_db'

I am new to laravel. I use homestead for development. I have two database connection
database.php
'cust_main_db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'project_db',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'admin_main_db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'projectadmin_db',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Homestead.yaml
databases:
- homestead
In my local mysql has both databases project_db and projectadmin_db
When I run the project http://homestead.app:8000/ it shows SQLSTATE[HY000] [1049] Unknown database 'projectadmin_db'
What I have missed here? Correct me if anything wrong.
When you run your app in Homestead, it looks for databases in Homestead and not your actual localhost. You can connect to Homestead's MySQL through 127.0.0.1:33060.
On models that use projectadmin_db database, you need to add
protected $connection = 'admin_main_db';
make sure you use the default Homestead port 33060 , when you are creating your databases ,
i.e: you created your databases on the guest vm not on the host machine.

Using Laravel, is there a way to check which database I'm connected to and change to another?

If I'm doing something simple like
Auth::user()->username
Is there a way I can change the database that I'm getting this information from?
I already have the other database connection information entered in the database.php file. Just not sure how to change it on the fly.
DB SETUP
'mysql2' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In your AUTH driver specify the DB to use
app/config/auth.php
'driver' => 'database',
for diff table than users
'table' => 'administrators',
Create your own driver
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver

Multi Database Migrations - How to set the connection details

I'm creating a multi-tenant application with Laravel 4. At the moment I have two connections in my database.php
'connections' => array(
'app' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
When connecting to a tenant database in the application I call something like this:
$tenant = Tenant::where('username', '=', $username)->first();
Config::set('database.connections.tenant.username', $tenant->db_username);
Config::set('database.connections.tenant.password', $tenant->db_password);
Config::set('database.connections.tenant.database', $tenant->db_database);
As you can see all my database names/connection details for tenants are set dynamically.
this is an issue when i'm trying to run a migration as I can't find a way to set the connection details.
The only way I can do this at the moment is to call my migration from a controller. Is their a better way?

How to use the CLI Options for Artisan to execute on a different Laravel Environment

I am installing the migrate scripts for the first time on my application.
What I wish to do is use the configuration under /config/test/database.php to run my migrate installation scripts.
My entry on paths.php is the following
$environments = array(
'test' => array('http://test.*'),
'local' => array('http://localhost.*')
);
My entry on /application/config/test/database.php
return array(
'connections' => array(
'mysql' => array('driver' => 'mysql',
'host' => 'localhost',
'database' => 'new_db',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '')
),
);
and /application/config/database.php
return array(
'connections' => array(
'mysql' => array('driver' => 'mysql',
'host' => 'localhost',
'database' => 'default_db',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '')
),
);
Every time I run php artisan migrate:install --env=test it always installs on the DB defined on /application/config/database.php and not using the configuration from /application/config/test/database.php.
Appreciate if someone could help me fix this issue.
I figured it out myself. Am not sure if this is something that has been documented in Laravel for multiple environment. An additional parameter was required on the environment definition on the paths.php
$environments = array(
'test' => array('http://test.*','MY_COMPUTER_NAME'),
'local' => array('http://localhost.*')
);
Now, running php artisan migrate:install --env=test correctly executes the migration scripts on the DB configuration defined on /application/config/test/database.php.
Hope this helps others

Resources