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()
...
Related
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]);
I'm trying to connect 2 database on my system with laravel 5, and when I try to get data from one I skip this error
Database [dpnmwin] not configured.
my file .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=spi_intranet
DB_USERNAME=root
DB_PASSWORD=null
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=dpnmwin
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=null
My file database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'spi_intranet'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'dpnmwin'),
'username' => env('DB_USERNAME_SECOND', 'root'),
'password' => env('DB_PASSWORD_SECOND', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
the error comes out when he tried to bring data from the dpnmwin database, as follows
public function index(){
$users = DB::connection('dpnmwin')->select('select * from datos_itu');
return view('users.list',array(
'users' => $users
));
}
but if I want to bring data from my other database spi_intranet
public function index(){
$users = User::all();
return view('users.list',array(
'users' => $users
));
}
it brings me the data without problems.
Why do not you bring me the data from my other database?
Is it a problem in the configuration?
You have to pass connection name, change this line and pass mysql2 as connection name.
$users = DB::connection('mysql2')->select('select * from datos_itu');
Read more here: https://laravel.com/docs/5.6/database#using-multiple-database-connections
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
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
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