Set connection according to user data logged - php

I created a second connection in my config/database.php and will also create a third connection, wanted to know how can I do to switch between these connections according to the logged in user.
config/database.php
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'database2' => [
'driver' => 'mysql',
'host' => ('localhost'),
'port' => ('3306'),
'database' => ('database2'),
'username' => ('root'),
'password' => (''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
I know I can choose my modem connection in that way, but how do you that name "database2" is pulled from the logged in user instead of being placed the name directly there?
class Empresa extends Model
{
protected $connection;
function __construct()
{
return $this->connection = 'database2';
}
}
I tried to put it that way, but it did not work.
return $this->connection = Auth::user()->database;
He gave this error.
ErrorException in Empresa.php line 16:
Trying to get property of non-object

Related

Problems specifying Laravel Eloquent database at runtime

Laravel 5.8. I am trying to save an Eloquent model, choosing the database connection at runtime:
$catModel = new Cat;
if (env('USE_STAGING')) {
$catModel->setConnection('staging');
}
$cat = $catModel::create(['name' => $request->name]);
My config/database.php file:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'staging' => [
'driver' => 'mysql',
'host' => env('STAGING_DB_HOST', '127.0.0.1'),
'port' => env('STAGING_DB_PORT', '3306'),
'database' => env('STAGING_DB_DATABASE', 'forge'),
'username' => env('STAGING_DB_USERNAME', 'forge'),
'password' => env('STAGING_DB_PASSWORD', ''),
'unix_socket' => env('STAGING_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
My problem is that each time I run this code, the cat is created in the mysql (default) database, rather than the staging database, even when USE_STAGING is true.
The problem is the static call of the create() method.
By changing $catModel::create() to $catModel->create() it should use the correct connection:
$catModel = new Cat;
if (env('USE_STAGING')) {
$catModel->setConnection('staging');
}
$cat = $catModel->create(['name' => $request->name]);

Laravel DB facade to use several DB

Below is how to use Facade for search table data with pagination in Laravel.
DB::table('customers')->paginate(15);
but it can be use just a DB,
So how to use several DB in Facade sentence?
I wish to use DB as divide as read and write DB or direct access in single sentence.
Thank you in advenced.
First of all you have to specify configuration for your databases in
.env:
DB_CONNECTION=mysql
DB_HOST=172.17.0.1
DB_DATABASE=my_database
DB_USERNAME=username
DB_PASSWORD=password
DB_SLAVE_HOST=172.17.0.1
DB_SLAVE_DATABASE=my_database_slave
DB_SLAVE_USERNAME=username2
DB_SLAVE_PASSWORD=password2
DB_MASTER_HOST=172.17.0.1
DB_MASTER_DATABASE=my_database
DB_MASTER_USERNAME=username
DB_MASTER_PASSWORD=password
and
project.dev/config/database.php:
'mysql_master' => [
'driver' => 'mysql',
'host' => env('DB_MASTER_HOST', 'localhost'),
'port' => env('DB_MASTER_PORT', '3306'),
'database' => env('DB_MASTER_DATABASE', 'forge'),
'username' => env('DB_MASTER_USERNAME', 'forge'),
'password' => env('DB_MASTER_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_slave' => [
'driver' => 'mysql',
'host' => env('DB_SLAVE_HOST', 'localhost'),
'database' => env('DB_SLAVE_DATABASE', 'forge'),
'username' => env('DB_SLAVE_USERNAME', 'forge'),
'password' => env('DB_SLAVE_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Then you can use method setConnection like that:
public function getIndex(Product $product)
{
$this->products = $product->setConnection('mysql_slave')
->where('id' '>' 20)
->get()
...

Laravel 5 Changing Database name in controller

ers. I need to change the database name into a specific controller. I already changed the database.php into this
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'loal'),
'database' => env('DB_DATABASE', 'test1'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'local'),
'database' => env('DB_DATABASE', 'test2'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
My main DB is test1 and I need to change it into test2 db name in here :
public function TransactionHistory(Request $request){
Config::set('database.default','sqlsrv2');
dd(DB::connection() );
}
But it only returns null and it is still reading test 1. Anyone?
One way is to change the connection by using DB::connection() method:
$connection = DB::connection('sqlsrv2'); //this will create a database connection using sqlsrv2 in your config.
Now, you can use $connection to run the queries, etc.
Reference :
If you are using Eluquent there is more elegant way Database Connection
Create enother config for second connection
`
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
);
`
Set connection in your Model:
`
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The connection name for the model.
*
* #var string
*/
protected $connection = 'connection-name';
}

Laravel 5.2 use two different database connections simultaneously (sqlsrv + mysql)

I would like to do something like this when i use a model
class DB extends Model {
Protected $table = "mssql_table";
}
DB::useConnection("mssql")->All();
As far as configuration goes i've found out that I can add it myself in app/config/database.php
And so I did.
So now i've got this in my connetions:
'mssql' => [
'driver' => 'sqlsrv',
'host' => env('DB_MSSQL_HOST', 'localhost'),
'port' => env('DB_MSSQL_PORT', '3306'),
'database' => env('DB_MSSQL_DATABASE', 'forge'),
'username' => env('DB_MSSQL_USERNAME', 'forge'),
'password' => env('DB_MSSQL_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and this to my .env file
DB_MSSQL_HOST=
DB_MSSQL_PORT=
DB_MSSQL_DATABASE=
DB_MSSQL_USERNAME=
DB_MSSQL_PASSWORD=
But what is the next step? google didnt really help me that much, in laravel 4.* you could use db::connection(); but it dosent seem to work anymore
Any ideas?
First, you need to set-up one or more databases in your config (be sure to change values, I just pretty much copied and pasted):
'db1' => [
'driver' => 'sqlsrv',
'host' => env('DB_MSSQL_HOST', 'localhost'),
'port' => env('DB_MSSQL_PORT', '3306'),
'database' => env('DB_MSSQL_DATABASE', 'forge'),
'username' => env('DB_MSSQL_USERNAME', 'forge'),
'password' => env('DB_MSSQL_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'db2' => [
'driver' => 'sqlsrv',
'host' => env('DB_MSSQL_HOST', 'localhost'),
'port' => env('DB_MSSQL_PORT', '3306'),
'database' => env('DB_MSSQL_DATABASE', 'forge'),
'username' => env('DB_MSSQL_USERNAME', 'forge'),
'password' => env('DB_MSSQL_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Then of course, you're going to need to create some migration schema for the newly added database (you need tables, etc.):
Schema::connection('db2')->create('table_name', function($table)
{
$table->increments('id');
...
});
Now in your Eloquent model, you can define what database you want to use like so:
class ModelName extends Eloquent {
protected $connection = 'db2';
}
Thank you #Mike Barwick, the only thing that i was looking for was protected $connection = ""
Now i can query 2 databases at once

How to connect to MySQL database on port 3308 using laravel framework 5.1?

I am trying to use laravel for the first time. I opned the database.php file located in the config directory and then update the mysql config.
but every time I try to do this command
php artisan migrate:install
I get this
[PDOException]
SQLSTATE[HY000] [2002] No connection could be made because the target machi
ne actively refused it.
I have to let laravel to connect to a different port somehow.
I have tried the following and none worked.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5'),
'port' => '3308',
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5:3308'),
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5'),
'port' => env('DB_PORT', '3308'),
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
finally, I tried this
'mysql' => [
'driver' => 'mysql',
'host' => '10.15.1.5:3308',
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
this gives me a different error
Access denied for user 'homestead'#'10.xxxxxx' (using password: YES)
I am not sure where is the user homestead is coming from.
How can I tell laravel to connect to mysql on port 3308?
I know you figured it out, but of all the attempts you provided, the answer you gave wasn't clear. For those looking in the future, here is what you need:
(This is assuming Laravel 5.1 using a Postgres DB, but should work with alternate versions of Laravel, and different DBs... also, don't mind the alternate/different config settings that my database.php has as opposed to yours, these were for advanced configurations.)
Add a 'port' section to your config/database.php, that looks like follows:
'pgsql' => [
'read' => [
'host' => env('DB_READ', 'localhost')
],
'write' => [
'host' => env('DB_WRITE', 'localhost')
],
'port' => env('DB_PORT', '5432'),
'driver' => 'pgsql',
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => env('DB_SCHEMA', 'public'),
'options' => array(
PDO::ATTR_PERSISTENT => env('DB_PERSISTENT', false),
),
],
Then in your .env you can override the port setting as follows:
DB_PORT=32769
I figured out the issue.
the file .env needs to be updated with the correct information

Categories