Laravel application trying to authenticate user using another database - php

I have two applications both developed in Laravel.I have logged into the first application and doing coding. But when I try to log into the second application it gives an error of the missing 'users' table(the table in the second database I am trying to log into now which is not in the first logged in an application).
Seems like the second application is trying to authenticate the user using the first database which is not correct
Can anyone assist me with this

Dear Laroja as i get you want to authenticate with another application which have separate database then you have to define two database auth and try to build the logic if connect to first application then it uses the first database and if you want to connect with another application it will connect to other database.
Please have a sapmle code for connect two databse
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'qmops_live_server',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => env('DB_PORT', '3306'),
'database' => 'XXX',
'username' => 'XXX',
'password' => 'QXXXX',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
please use the above code in app/config/database.php hope my solution will help you Thanks.

Related

How to pass the value of a variable between 2 different sites

So. There is 2 different sites.
site_1 and site_2
Site_1:
PHP 7.3
Laravel 6.0
MYSQL
Contains corporate portal with helpdesk,news and so.
Site_2:
PHP 7.2
Laravel 5.6
MYSQL
Contains Videoportal(yeah like youtube:)) with users and webcams from construction sites.
On site_1 i have a class User with some properties (i.e. user id,department,and so) contains in db.
On site_2 i have different DB with user_id and cams .I want to select only webcams that belongs to user from site_1.
How can i pass value of User_id from site_1 to site_2 to select cams only for exact user?
In database.phpm define second mysql connection as
<?php
return array(
'default' => 'mysql',
'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' => '',
),
),
);
Then use
$users = DB::connection('mysql2')->select('select * from users');
Hint
https://laravel.com/docs/5.7/database#using-multiple-database-connections
Are they using the same database or are the databases on those apps synced? If not, then this is a bad practice. Well as for passing values from one Laravel app to another, just create endpoints wherein it will send/accept values.
Read the docs especially on the Controllers and Routing and then study on how you will implement it using your own logic.

Connect to Different Database in October CMS

Connecting to Different Database in October CMS
Hello,
I have two databases first one was created by OctoberCMS during installation process and the other one which i had already. how do i connect to that other database, i tried using builder plug-in but i am confused how to do it.
I want to connect to the other database and fetch information from one table and display on my home page.
Please help me with this. I am newbie to this CMS.
Thank you.
In your config/database.php you will have something like this (assuming MySQL):
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
Just add another one after it, changing the name to something else (mysql-legacy in my example below) and the relevant connection details:
'mysql-legacy' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
After that, you can use DB; in your model or controller and then do things like this:
$query = DB::connection('mysql-legacy')->select('SELECT * FROM tablename');

Illuminate Database connectivity with mysql router

I'm using Laravel's illuminate database library outside laravel with jessengers mongodb.
My requirement is multiple database connectivity through illuminate database.
Currently, I've added two connection one mysql and one mongodb.
To split the database load, I need to connect to mysql router instead of mysql db server directly. Also, in that I want one only for Read operation and one for Read/Write operation.
Kindly help me out on this.
Thanks in advance.
Current connections
$db = new Capsule;
$db->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'test',
'username' => 'test',
'password' => 'test#123#',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "default");
$db->addConnection([
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'test',
'username' => null,
'password' => null,
'options' => []
], "mongodb");
$db->getDatabaseManager()->extend('mongodb', function ($config) {
return new Connection($config);
});
$db->setEventDispatcher(new Dispatcher(new Container));
$db->setAsGlobal();
$db->bootEloquent();
I need to replace one mysql connection with two mysql connection for Read and Read/Write operation through mysql router.
You can define read/write options separately with either mysql host or mysql router host and port
$db->addConnection([
'driver' => 'mysql',
'read' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'write' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'database' => '<mysql_database>',
'username' => '<mysql_database_user>',
'password' => '<mysql_database_password>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "mysql");

Seeding tenant database in Laravel 5

I am working on building a system using a main database and multiple tenant databases in laravel 5. I have a database migration and seed for the main database no problem, using php artisan migrate:refresh --seed.
Tenants then get their own database upon registration. I need to run a migration and seed on the tenants database.
The tenant migration files are stored in a separate folder. The migration runs (unfortunately on the main database) with the following command
\Artisan::call('migrate', [
'--path' => "database/migrations_system"
);
However I need the migration to occur on the tenant database, say DB_1.
I read the following should work
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'db_1'
]);
However I end up with an error
InvalidArgumentException in DatabaseManager.php line 238:
Database [db_1] not configured.
Stuck.... How can I specify the migration to run on a specific database?
UPDATE:
I have found that changing config/database.php and adding db_1 gets me past this error....
'db_1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db_1',
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
],
However this connection is unknown and needs to be done on the fly, which I am also stuck on how to accomplish....
UPDATE Again and working solution.... Modify the config on the fly...
$connections = \Config::get('database.connections');
$tenant_database = 'db_1'; //assign from your main database
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $tenant_database ,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections['tenant'] = $tenant_connection;
\Config::set('database.connections', $connections);
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'tenant'
]);
So it looks like I have answered my own question, however maybe some can comment on this procedure.
I'll put my own solution down....
1) Create the database name
For example I name the new database db_1 where 1 represents the ID of the system in the main database. I have considered storing a unique scrambled name in the main database. In the end that seemed like overkill.
2) Create the connection - for this I use the exact same name as the new database.
public function createConnection()
{
$connections = \Config::get('database.connections');
if(!isset($connections[$this->getSystemName()]))
{
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $this->getSystemName(),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections[$this->getSystemName()] = $tenant_connection;
\Config::set('database.connections', $connections);
//dd(\Config::get('database.connections'));
}
}
3) Create the database, which you do using your main connection
$sql = "CREATE DATABASE " . $this->tenant_name;
DB::connection('main_db')->statement($sql);
4)run the migration
$r = \Artisan::call('migrate', [
'--path' => "database/migrations_tenant",
'--database' => $this->tenant_name
]);
5) finally you can seed test data or load some default data. Access the connection like
DB::connection($this->system_name)->table($table)->insert($csv);

how to change database name in laravel? changing config>database.php doesn't work

I created a database "mydatabase" and I changed config>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
now inside route.php i have:
Route::get('/', function()
{
$data=DB::table('user')->get();
return $data;
});
laravel sends an Exception which shows that it tries to access:
homestead.user
instead of
mydatabase.user
now if i change route.php to:
Route::get('/', function()
{
$data=DB::table('mydatabase.user')->get();
return $data;
});
it will work!
Also according to this question I changed config>local>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
But this time, even
$data=DB::table('mydatabase.user')->get();
doesn't work either! This time it thrown another exception :
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused
My question is why laravel tries to use "homestead" database instead of "mydatabase"? should I change something else?
EDIT:
I changed the config/local/database.php to
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and everything works fine! (I changed mysite.local to localhost)
I've not define local host in my /etc/hosts so why laravel looks for that host?
Your host should be localhost. The term localhost means the computer which laravel is running on. mysite.local is presumably a virtual site residing on this computer. It doesn't have its own installation of Mysql. All virtual sites will share the same mysql. They will just use different databases.
Thats how my setups work anyway.
The problem is in your config/database.php with default connection, currently default connection setting is getting from .env file as
'default' => env('DB_CONNECTION', 'mysql'),
So change it to :
'default' => 'mysql',

Categories