Change request default database in laravel 5.1 - php

I make Request to Validate requested values from controller action by this way:
namespace App\Http\Requests;
use App\Http\Requests\Request;
class AccountsRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize() {
return TRUE;
}
public function rules() {
return [
'email' => 'email|required|max:255|unique:accounts',
'password' => 'min:6|required'
];
}
}
All is fine if I'm using the default database, but for this Validation I need to check the tables in other database. In config I have two databases:
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'sait'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'account'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
When I call the Validation, it is checking by "unique" rule in my default database so I need to change it, but I cannot found anywhere how to do this.

According to the docs unique - Custom Database Connection
instead of
'email' => 'email|required|max:255|unique:accounts',
you need to do
'email' => 'email|required|max:255|unique:mysql2.accounts',
I guessed its mysql2 as you didnt mention.

Did you see this in the docs for the 'unique' rule?
$verifier = App::make('validation.presence');
$verifier->setConnection('connectionName');
$validator = Validator::make($input, [
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users',
]);
$validator->setPresenceVerifier($verifier);
You can use the setConnection() method to specify how you want unique to be determined.

Related

Laravel 5.2 - Make different Read/Write Connections

In laravel 5.2, we wanted to have different connections for read/write, so I followed the advice as provided in laravel documents. But, by default, it was only creating default mysql named connection and not 2 different read/write connections, therefore it was picking the read connection for operations like INSERT/UPDATE.
After debugging, it was found that in DatabaseManager.php file the connection named passed as argument to makeconnection() was mysql and not mysql::read or mysql::write.
Before
config/database.php
'mysql' => [
//we need to have this nested options for both read/write
'read' => [
'host' => env('DB_READ_HOST'),
],
'write' => [
'host' => env('DB_WRITE_HOST'),
],
'host' => env('DB_READ_HOST'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
File - vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php
public function connection($name = null)
{
list($name, $type) = $this->parseConnectionName($name);
// If we haven't created this connection, we'll create it based on the config
// provided in the application. Once we've created the connections we will
// set the "fetch mode" for PDO which determines the query return types.
if (! isset($this->connections[$name])) {
$connection = $this->makeConnection($name);
$this->setPdoForType($connection, $type);
$this->connections[$name] = $this->prepare($connection);
}
return $this->connections[$name];
}
So, we have added a small change in the file, after adding this, it's now creating two different connections mysql.read/mysql.write and switching them appropriately according to the given sql operations SELECT,INSERT,UPDATE
Needed you feedback if this is a viable solution ?
After changing the file
config/database.php
'mysql' => [
//we need to have this nested options for both read/write
'read' => [
'host' => env('DB_READ_HOST'),
'username' => env('DB_READ_USERNAME'),
'password' => env('DB_READ_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
'write' => [
'host' => env('DB_WRITE_HOST'),
'username' => env('DB_WRITE_USERNAME'),
'password' => env('DB_WRITE_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
'host' => env('DB_READ_HOST'),
'username' => env('DB_READ_USERNAME'),
'password' => env('DB_READ_PASSWORD'),
'driver' => 'mysql',
'database' => env('DB_DATABASE'),
'collation' => 'utf8_unicode_ci',
'port' => env('DB_PORT', '3306'),
'charset' => 'utf8',
'prefix' => '',
'strict' => false,
],
File - vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php
public function connection($name = null)
{
list($name, $type) = $this->parseConnectionName($name);
// we check if the $type is read/write and store appropriate connections
if( $type != null ) {
$name .= '.' . $type;
}
//end
// If we haven't created this connection, we'll create it based on the config
// provided in the application. Once we've created the connections we will
// set the "fetch mode" for PDO which determines the query return types.
if (! isset($this->connections[$name])) {
$connection = $this->makeConnection($name);
$this->setPdoForType($connection, $type);
$this->connections[$name] = $this->prepare($connection);
}
return $this->connections[$name];
}

Error: [InvalidArgumentException] Database [fooBar] not configured Laravel 5.4

I'm trying to write a migration for a new table.
public function up()
{
Schema::connection('fooBar')->create('tableOfThings', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
But when I run php artisan migration, I get the following error.
[InvalidArgumentException]
Database [fooBar] not configured.
This is what I have in config/database.php. You can see that I do infact have this DB configured.
'connections' => [
'foo_bar' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 3306),
'database' => 'fooBar',
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
// ...
]
Make sure the name of the configuration (not the database) is what you are passing to the connection.
DB::connection('foo_bar');
'connections' => [
THIS 'foo_bar' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 3306),
NOT THIS 'database' => 'fooBar',
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
// ...
]
You are trying to connect to the fooBar connection, but your connections array doesn't have that connection. It's called foo_bar instead.
Either change your connections configuration to fooBar or your schema connection call to foo_bar.
'connections' => [
'fooBar' => [ // Changed from foo_bar to fooBar.
// ...
],
// ...
]

Set connection according to user data logged

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

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

Categories