Laravel connect 2 different databases - laravel

I want to connect in two differents databases, one mysql and other sql server
I got all the configuration and mysql works, sql server return nothing else but a white screen
Controller
Config::set('database.default', 'sqlsrv');
$teste = DB::connection('sqlsrv')->getDatabaseName();
dd($teste);
database config
'default' => 'mysql',
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'xxx.xxx.xxx.xxx',
'database' => 'MP11_OFICIAL02',
'username' => 'sa',
'password' => 'asd',
'prefix' => '',
),
'MG' => array(
'driver' => 'mysql',
'host' => 'xxx.xxx.xxx.xxx',
'port' => '3306',
'database' => 'bd',
'username' => 'sistema',
'password' => 'asd',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and also if I dont change my 'default' => 'mysql' I get this error
could not find driver (View: /vagrant/app/views/usuarios/index.blade.php)
and if I change to sqlsrv just get a white screen

What version of SQL Server Native Client you are using? Please go to Control Panel >> Administrative Tools >> Data Sources (ODBC) and then click on the Drivers tab to find out which SQL Server Native Client you have already installed. Also please check your php.ini if you already install PDO driver for sqlsrv. Find 'PDO drivers' in php.ini. If you already install, you can see it will list like this. PDO Drivers => sqlsrv, mysqlsqlite

Related

Work with mysql database in Laravel 4.2

I am new to Laravel used this https://github.com/msurguy/laravel-facebook-login link to work with facebook login in Laravel. I have setup all the things but i
got following errors when establishing database connection :
Type error: Argument 1 passed to
Illuminate\Redis\Database::__construct() must be of the type array,
null given, called in
C:\wamp64\www\laravel-facebook-login-master\vendor\laravel\framework\src\Illuminate\Redis\RedisServiceProvider.php
on line 23
I want to run simple mysql database. Below is my database.php code:
return array(
'fetch' => PDO::FETCH_CLASS,
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'facebookdb',
'username' => 'root',
'password' => '',
'port' => '3603',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
),
'migrations' => 'migrations',
);
Any suggestion/help will be highly appreciated.
You are missing the definition for your redis connection in that connections array, something like this will help:
'redis' => array(
'host' => env('REDIS_HOST', 127.0.0.1),
'port' => env('REDIS_PORT', 6379),
'password' => env('REDIS_PASSWORD', null),
'database' => env('REDIS_DATABASE', 0)
)
Make sure that you add the corresponding entries in your .env file if your redis connection settings differ from the default values above.

Google Cloud app engine with Laravel: unix_socket issue

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

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

Laravel using different connections for Inserting & Selecting data

Is there anyway in the Laravel Eloquent to use two different connections, for inserting, updating and Selecting.
What i am trying to do is specify a connection when user is pulling the data from db, and another one while inserting or updating data.
I am wondering if it can be done with the Eloquent instead of defining connections everytime?
This is possible with Laravel 4.1. You can configure it in your app/config/database.php like so:
'mysql' => 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' => '',
),
See the Read / Write Connections section in the Laravel database documentation.

Resources