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.
Related
In a multi-tenant Laravel app, each tenant has its own database connection. So after the user has selected his database, I want to authenticate the user using Auth::loginUsingId. Still, no matter what I do, I cannot change the Users Model's connection to another default.
If I set the connection in the model, it does connect to the specific database, but I want this to be done dynamically.
Is there a way to specify the connection dynamically that Laravel's auth should use for the authentication?
You could define another connection in your config/database.php file like this:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'second_db_connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
And change the User model to be like this:
class User extends Model {
protected $connection = 'second_db_connection';
}
Before authenticating the user, change the database connection temporarily for the current request only using Conifg::set
$db = "database_name";
Config::set("database.connections.mysql.database", $db);
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.
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
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
I'm working on a Laravel 4 based site which works over multiple databases. There is one query I need to run on each request that pulls a record from a different database.
Is there a way I can somehow tie this particular model to the other database so I can just retrieve it as usual?
$client = Client::find(Session::get('client_id'));
Any advice appreciated.
Thanks
// Model
class Client extends Eloquent
{
protected $connection = 'masterDb';
}
// config/database.php
'masterDb' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'name',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
You can create as many named connections as you wish. Set one of them as default,
each model can use any of these connections later.