Laravel sync offline database to online - php

I want to sync my local database to online database when i click on a button in blade page. I looked online for the solution but did not found anything that worked for me.
I have updated my database .php file I have added local and server database connection credentials.
I have found a solution but that is not completed.
solution that i found
database.php
'local' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('LOCAL_DB_HOST', '127.0.0.1'),
'port' => env('LOCAL_DB_PORT', '3306'),
'database' => env('LOCAL_DB_DATABASE', 'forge'),
'username' => env('LOCAL_DB_USERNAME', 'forge'),
'password' => env('LOCAL_DB_PASSWORD', ''),
'unix_socket' => env('LOCAL_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'server' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('SERVER_DB_HOST', '127.0.0.1'),
'port' => env('SERVER_DB_PORT', '3306'),
'database' => env('SERVER_DB_DATABASE', 'forge'),
'username' => env('SERVER_DB_USERNAME', 'forge'),
'password' => env('SERVER_DB_PASSWORD', ''),
'unix_socket' => env('SERVER_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
controller code
// Connect to local database
return $local_database = DB::connection('local-db')->table('table_name');
// Get table data from production
foreach ($local_database->table('table_name')->get() as $data) {
// Save data to staging database - default db connection
DB::table('table_name')->insert((array) $data);
// This query can do only one time for multiple time we have to do
DB::table('table_name')->insertOrUpdate((array) $data);
// This query also update column only, if you do some changes in
//previous data it will not sync for that i dont know this query is
//correct or not but it worked.
DB::table('table_name')->delete((array) $data);
DB::table('table_name')->insertOrIgnore((array) $data);
}
env
SERVER_DB_CONNECTION=server
SERVER_DB_HOST=127.0.0.1
SERVER_DB_PORT=3306
SERVER_DB_DATABASE=sync_db
SERVER_DB_USERNAME=root
SERVER_DB_PASSWORD=
LOCAL_DB_CONNECTION=local
LOCAL_DB_HOST=127.0.0.1
LOCAL_DB_PORT=3306
LOCAL_DB_DATABASE=sync_db
LOCAL_DB_USERNAME=root
LOCAL_DB_PASSWORD=

Related

How to connect with another database to submiit the data in laravel

I am working on a cms project. I was created a portal and thenn new database connection created. So,there is a table users i wan that if someone create new user in that portal then it will be added to the master database that is another database. So, in laravel how can we make connection with another database and save the data of the portal to our master database table also.
Trying to make a sync connection that if in cms portal if admin create a user then that data must be submitted to the master table.
You can do it like this:
In .env add the following: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=user
DB_PASSWORD=password
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=localhost
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=db2
DB_USERNAME_SECOND=user2
DB_PASSWORD_SECOND=password2
In config/database.php: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
In the sample model:
class Job extends Model
{
protected $connection = 'mysql2';
}

How to use two different database one from sql server and another from mysql server in same laravel application

I am developing a laravel application. I connect my laravel application with a mysql database. But I need a table data that is in another server and that database is sql database is in IIS server.
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=line
DB_USERNAME=root
DB_PASSWORD=
I need help to use two databases from different server (one from mysql and another from sql) on my same laravel application.
In your config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
// Add this (Connect sql server)
//TCP port 1433 is typically the port used by a default instance of SQL Server
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env(''),
'host' => env('DB_HOST', 'X.X.X.X'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8mb4',
'prefix' => '',
'prefix_indexes' => true,
],
Get your mysql table
$mysql = DB::connection('mysql')->table('xxx')->select('*')->get();
Get your sql server table
$sqlsrv= DB::connection('sqlsrv')->table('xxx')->select('*')->get();

"Access denied for user, using password: yes" Laravel 8 Heroku

Here is the config/database.php file that I modified using this tutorial. It says that I need to attach the $DATABASE_URL=parse_url("mysql://####"); code block above the said php page. (the tutorial is using postgreSQL, while my system needs to use MySQL).
// attached $DATABASE_URL=parse_url("mysql://####") on the topmost part of the page
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => $DATABASE_URL['host'], // edited this line
'port' => $DATABASE_URL['port'], // edited this line
'database' => ltrim($DATABASE_URL['path'], '/'), // edited this line
'username' => $DATABASE_URL['user'], // edited this line
'password' => $DATABASE_URL['pass'], // edited this line
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
below is the original mysql code
// 'mysql' => [
// 'driver' => 'mysql',
// 'url' => env('DATABASE_URL'),
// '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' => '',
// 'prefix_indexes' => true,
// 'strict' => true,
// 'engine' => null,
// 'options' => extension_loaded('pdo_mysql') ? array_filter([
// PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
// ]) : [],
// ],
error ouput when running the system with the said problem
SQLSTATE[HY000] [1045] Access denied for user '{{redacted username}}'#'{{redacted ip address}}' (using password: YES) (SQL: select * from video_links where home_video = true and video_links.deleted_at is null limit 1)
Another problem I encounter while running the migration command in the Heroku CLI is this:
PHP Warning: Undefined array key "port" in /app/config/database.php on line 51
It seems like the $DATABASE_URL=parse_url("mysql://####") is not fetching the port value in its array.
How should I approach this?
You can leave config/database.php unchanged.
And add an ENV var DATABASE_URL:
In the settings or via CLI:
heroku config:set DATABASE_URL=YOUR_URL

store sessions into a common database

Basically what I am trying to do is sharing session between laravel applications using a common database.
what I've tried so far is
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=login_test
DB_USERNAME=root
DB_PASSWORD=
DB_DATABASE_ADMIN=login_admin
DB_USERNAME_ADMIN=root
DB_PASSWORD_ADMIN=
SESSION_DRIVER=database
SESSION_CONNECTION=login_admin //getting error here: Database [login_admin] not configured.
session.php
'driver' => env('SESSION_DRIVER', 'file'),
'files' => storage_path('framework/sessions'),
'connection' => env('SESSION_CONNECTION', null),
'table' => 'sessions',
database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_ADMIN', 'forge'),
'username' => env('DB_USERNAME_ADMIN', 'forge'),
'password' => env('', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
]
so here I want to set the sessions table into my common database named login_admin
I am already sharing a common users table between the projects so I've no issue on connecting multiple databases.
I've checked previous relevant questions but did not find how to share the table into common database
/*
|--------------------------------------------------------------------------
| Session Database Connection
|--------------------------------------------------------------------------
|
| When using the "database" or "redis" session drivers, you may specify a
| connection that should be used to manage these sessions. This should
| correspond to a connection in your database configuration options.
|
*/
According to your code SESSION_CONNECTION field should be the connection name, which is mysql2 in your case and not login_admin

Laravel issue in join query

here is my code
return DB::table('jobs')->Join('cities','jobs.city_id','=','cities.city_id')
->select('jobs.city_id', DB::raw('COUNT(jobs.city_id) AS num_jobs'))
->groupBy('jobs.city_id')
->orderBy('cities.city')
->limit($limit)
->get();
I want to get result order by alphabetically cities name which is in cities table row name as city and city_id in both tables jobs and cities, I also want number of jobs to show number of jobs in a specific city.
Thanks,
Change strict value to false in database.php file. It will works.
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => false, //Change this
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

Categories